Wordwrap () ... Split only word with specific length

I'm trying to use wordwrap()

to break the text, but I don't want it to break the text every time it reaches a certain length, I want it to only break words that exceed a certain length, so for example:

<?php

$string = "Some text that will be splited each 15 characters...";
echo wordwrap($string,15,"<br>");
?>

      

Well it works, but I would like to break only words that have exceeded a certain length, for example:

<?php

$string = "This string contains word Entertainment, and this word has 13 characters";
//I want the wordwrap to split only the words that exceed the limit so..
wordwrap($string,10,"<br>");
//Wont work as I expect...
?>

      

What can I do? Thank!

Expected Result:

This line contains the word Entertainm

ent and this word has 13 characters

+3


source to share


1 answer


Use the last parameter $cut

to shorten long words:

wordwrap($string, 10, "<br>", true); // true in the last param cuts long words

      



The default is false

(dont break long words).

+2


source







All Articles