Insert line break in long string. 15Mar, 2012

Here is the function stringWrap to insert <br> in long string as a single word according to the charachters mentioned.

Parameters : $string : String to insert line breaks(In this example : ‘rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr’ is string)

$length : after how many characters line breaks( <br> ) will insert specified(In this example : ‘5’ is length)

Here is the Code:

function stringWrap( $string, $length){
$subStr = $string ;
$myString = ”;
$lastIndex = 0 ;
$subStr = substr($string,$lastIndex,$length) ;
$lastIndex += $length;
$myString .= $subStr .'<br/>’;


return $myString ;


echo stringWrap( ‘rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr’, 5); //calling function



Posted by: naresh / In: PHP

One thought on “Insert line break in long string.”

Comments are closed.