1

Possible Duplicate:
PHP: str_split without word-wrap

Let's say I have this string :

$string = "Lorem ipsum dolor sit amet";

and I have character limit on my database : 15 characters. by using str_split($string,15), I got this result :

Array
(
[0] => Lorem ipsum dol
[1] => or sit amet
)

this looks ugly to me... how to cut string to the closest word using PHP but the result is like this :

Array
(
[0] => Lorem ipsum
[1] => dolor sit amet
)
1

1 Answer 1

9

You can use wordwrap and explode

$string = "Lorem ipsum dolor sit amet";
$string = wordwrap($string, 15, ";;", true);
var_dump(explode(";;", $string));

Output

array
  0 => string 'Lorem ipsum' (length=11)
  1 => string 'dolor sit amet' (length=14)
Sign up to request clarification or add additional context in comments.

2 Comments

could you tell what will you do if the string is like "loremipsum Registered User"
you can check it your self : codepad.viper-7.com/rP2HRL

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.