php - Split a string by number of chars without splitting a word -
i want explode string (for example) 50 symbols (min), without dividing words in 2 parts.
is possible?
using str_split()
cause lastly word split, don't want.
example: splitting string 5 symbols;
$input = 'this example, illustration can anything.'; $output[0] = 'this'; $output[1] = 'is example,'; $output[2] = 'example'; $output[3] = 'can'; $output[4] = 'be anything';
i don't think there's single built-in function you, this:
codepad illustration here
$string = "lorem ipsum dolor sit down amet, consectetur adipiscing elit. quisque nec elit dui, nec fermentum velit. nullam congue ipsum ac quam auctor nec massa nunc."; $output = array(); while (strlen($string) > 50) { $index = strpos($string, ' ', 50); $output[] = trim(substr($string, 0, $index)); $string = substr($string, $index); } $output[] = trim($string); var_dump($output); // array(3) { // [0]=> // string(50) "lorem ipsum dolor sit down amet, consectetur adipiscing" // [1]=> // string(55) "elit. quisque nec elit dui, nec fermentum velit. nullam" // [2]=> // string(43) "congue ipsum ac quam auctor nec massa nunc." // }
php explode strsplit
No comments:
Post a Comment