Created
November 20, 2012 16:26
-
-
Save acoustep/4119009 to your computer and use it in GitHub Desktop.
for slicing at to end of words
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function slice_word($string, $chars=3) | |
{ | |
//1 trim and get rid of html | |
//check if length more than required | |
//2 cut to certain length | |
//3 do normal array stuff but take off the last array unless only 1 or 2? | |
$string = trim(strip_tags($string)); | |
if(strlen($string) > $chars) | |
{ | |
$cut_string = substr($string, 0, $chars); | |
$string_array = explode(' ',$cut_string); | |
$array_count = count($string_array); | |
if($array_count > 1) | |
{ | |
$string_array_minus_one = array_slice($string_array,0,$array_count - 1); | |
$string_array_minus_one = implode(' ', $string_array_minus_one); | |
$string_array_minus_one = trim($string_array_minus_one); | |
if($string_array_minus_one[strlen($string_array_minus_one)-1] == "-" || $string_array_minus_one[strlen($string_array_minus_one)-1] == "&") | |
{ | |
$string_array_minus_one = substr_replace($string_array_minus_one,"",-1); | |
$string_array_minus_one = trim($string_array_minus_one); | |
} | |
return $string_array_minus_one.'...'; | |
} | |
else | |
{ | |
return implode($string_array); | |
} | |
} | |
else | |
{ | |
return $string; | |
} | |
/* $stringDisplay = trim(substr(strip_tags($string), 0, $length)); | |
if (strlen(strip_tags($string)) > $length) | |
$stringDisplay .= '...';*/ | |
$wordArray = array_slice($string_array,0,$length); | |
$string = implode(' ', $wordArray); | |
if(count($string_array)>$length) | |
return $string.'...'; | |
else | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment