Cut a string recursively
I have a string, and I must print it in a div.
The width of the div is limited, so if many characters are close the string will exit from the div.
Just think to a width:30px
div and a string such as hellomynameismarcoandilikemadewebapplicationinmyfreetimes
.
So I need a sort of function on php that cut recursively the string; I mean, cut the string and add somethings like <br/>
.
Another problem here is that my string is printed through htmlentities($string, ENT_QUOTES, "UTF-8");
, so the <br/>
will fail.
How can I do it on PHP?
EDIT My Final Solution
function printMyStrings($str, $width) {
return ereg_replace("<br/>","<br/>", htm开发者_C百科lentities(wordwrap($str, $width, "<br/>", true), ENT_QUOTES, "UTF-8"));
}
chunk_split ( $string , 80 ,"<br>");
The following will hard-wrap it at $width
. Docs on wordwrap()
$width = 30;
$wrapped_string = wordwrap("Hellomynameismarcoandilikemadewebapplicationinmyfreetimes.", $width, "\n", TRUE);
// nl2br to add HTML linebreaks
echo nl2br($wrapped_string);
Just add a space and the browser will take care of breaking it for you
精彩评论