Zend - The PHP Company




Text

Add Code


Text wrapper with regexp  

Type: code fragment
Added by: fabricet
Entered: 24/07/2000
Last modified: 01/12/2001
Rating: **** (3 votes)
Views: 5494
wrap_text is a basic function to wrap a given text argument to a certain number of characters. To be customized.


<?php
function wrap_text($text$wrap) {

$wrap -= 2//wrapped phrase is started and ended by a char

preg_match_all("/bw.{1,$wrap}wb/",$text,$parts);

return (
implode("<br>"$parts[0])); 
}
?>


Usage Example


$phrase = "This is a very long string only with spaces, alphanumerics,  it takes over 72 characters on a single line,  it should be wrapped"; 

print $phrase . "<br>";

print (wrap_text($phrase, 14));


Rate This Script





Search



This Category All Categories