note 92577 modified in function.wordwrap by thiago

From: Date: Thu, 30 Jul 2009 12:06:01 +0000
Subject: note 92577 modified in function.wordwrap by thiago
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-158911@lists.php.net to get a copy of this message
I tried using mb functions to word wrap 10 long strings (200 unicode utf8 chars) and I hit execution timeout error. After searching and testing, I came up with this simple and fast function for unicode word wrap. <?php //function to break up strings without whitespace chars in between //if $cut is true then strings will be forcefully break up even there are whitespace in between. function unicode_wordwrap($str, $len=50, $break=" ", $cut=false){ if(empty($str)) return ""; $pattern=""; if(!$cut) $pattern="/(\S{".$len."})/u"; else $pattern="/(.{".$len."})/u"; return preg_replace($pattern, "\${1}".$break, $str); } ?> --was-- I tried using mb functions to word wrap 10 long strings (200 unicode utf8 chars) and I hit execution timeout error. After searching and testing, I came up with this simple and fast function for unicode word wrap. //function to break up strings without whitespace chars in between //if $cut is true then strings will be forcefully break up even there are whitespace in between. function unicode_wordwrap($str, $len=50, $break=" ", $cut=false){ if(empty($str)) return ""; $pattern=""; if(!$cut) $pattern="/(\S{".$len."})/u"; else $pattern="/(.{".$len."})/u"; return preg_replace($pattern, "\${1}".$break, $str); } http://php.net/manual/en/function.wordwrap.php

« previous php.notes (#158911) next »