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 ;
while($subStr){
$subStr = substr($string,$lastIndex,$length) ;
$lastIndex += $length;
$myString .= $subStr .'<br/>’;

}

return $myString ;

}

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

OUTPUT:

rrrrr<br/>rrrrr<br/>rrrrr<br/>rrrrr<br/>rrrrr<br/>rrrrr<br/>rrrrr<br/>rrrrr<br/>rrrrr<br/>rrrrr<br/>rrrrr<br/>rrrrr<br/>rrrrr<br/>

Posted by: naresh / In: PHP
Cam

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

Comments are closed.