zohab Posted December 8, 2009 Share Posted December 8, 2009 Hi, I want to breaks string using <br/> after n numbers of characters. I have use wordwrap function but it does not work with no space string. echo wordwrap("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccchghggggggggggggggggf",10, "<br />\n"); Quote Link to comment https://forums.phpfreaks.com/topic/184368-wordwrap-help/ Share on other sites More sharing options...
vinpkl Posted December 8, 2009 Share Posted December 8, 2009 wordwrap will work if you have spacing between your words. at present you have only one word. <?php $text = "The quick brown fox jumped over the lazy dog."; $newtext = wordwrap($text, 20, "<br />\n"); echo $newtext; ?> Quote Link to comment https://forums.phpfreaks.com/topic/184368-wordwrap-help/#findComment-973285 Share on other sites More sharing options...
zohab Posted December 8, 2009 Author Share Posted December 8, 2009 Is there any way to break single long string? Quote Link to comment https://forums.phpfreaks.com/topic/184368-wordwrap-help/#findComment-973288 Share on other sites More sharing options...
premiso Posted December 8, 2009 Share Posted December 8, 2009 Is there any way to break single long string? One way to do it: <?php $string = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; echo breakString($string, "<br />\n", 15); function breakString($string, $delimeter = "<br />\n", $breakAt=15) { $breaks = ceil(strlen($string) / $breakAt); // break every 15 characters $newString = ""; for ($i=0; $i<$breaks; $i++) { $newString .= substr($string, ($i * $breakAt), $breakAt) . $delimeter; } return $newString; } ?> EDIT: Made it into a function. Quote Link to comment https://forums.phpfreaks.com/topic/184368-wordwrap-help/#findComment-973482 Share on other sites More sharing options...
Philip Posted December 8, 2009 Share Posted December 8, 2009 Why not just use the last parameter of wordwrap? It'll cut long words if you pass true to its value echo wordwrap("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccchghggggggggggggggggf",10, "<br />\n", true); Quote Link to comment https://forums.phpfreaks.com/topic/184368-wordwrap-help/#findComment-973486 Share on other sites More sharing options...
premiso Posted December 8, 2009 Share Posted December 8, 2009 Why not just use the last parameter of wordwrap? It'll cut long words if you pass true to its value echo wordwrap("cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccchghggggggggggggggggf",10, "<br />\n", true); lol because I did not think to look at that and I thought it would be fun to write a function, but yea. That is definitely the better solution! Quote Link to comment https://forums.phpfreaks.com/topic/184368-wordwrap-help/#findComment-973490 Share on other sites More sharing options...
Philip Posted December 8, 2009 Share Posted December 8, 2009 haha good job. but if for some reason wordwrap is taken out of the core on your system, premiso's function should work hehe Oh and congrats on 5k posts Quote Link to comment https://forums.phpfreaks.com/topic/184368-wordwrap-help/#findComment-973493 Share on other sites More sharing options...
premiso Posted December 8, 2009 Share Posted December 8, 2009 Oh and congrats on 5k posts Oh wow, didn't even notice that lol. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/184368-wordwrap-help/#findComment-973496 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.