[pLog-svn] r3834 - plog/trunk/class/template/smarty/plugins
mark at devel.lifetype.net
mark at devel.lifetype.net
Wed Aug 2 14:55:30 GMT 2006
Author: mark
Date: 2006-08-02 14:55:29 +0000 (Wed, 02 Aug 2006)
New Revision: 3834
Modified:
plog/trunk/class/template/smarty/plugins/modifier.utf8_wordwrap.php
Log:
Change the utf8_wordwrap to a better version. You can find it here: http://milianw.de/section:Snippets/content:UTF-8-Wordwrap
1. completly the same syntax as the original wordwrap function: string utf8_wordwrap(string $str, integer $width, string $break [, bool $cut]);
2. The $cut parameter is supported (tjomi4's function only supports $cut = true).
But be careful: I use regular expression word boundaries (\b) for this feature. I'm not sure if this works everywhere!
3. The function uses the multibyte extension if installed for counting the string length
4. The regular expression inside the while loop is shorter and uses preg_match() instead of preg_replace(). That should improve performance and prevent a strange bug (Compilation failed: regular expression too large)
Modified: plog/trunk/class/template/smarty/plugins/modifier.utf8_wordwrap.php
===================================================================
--- plog/trunk/class/template/smarty/plugins/modifier.utf8_wordwrap.php 2006-08-02 09:58:16 UTC (rev 3833)
+++ plog/trunk/class/template/smarty/plugins/modifier.utf8_wordwrap.php 2006-08-02 14:55:29 UTC (rev 3834)
@@ -23,25 +23,40 @@
*/
function smarty_modifier_utf8_wordwrap($string,$length=80,$break="\n",$cut=false)
{
- return utf8_wordwrap($string,$length,$break);
+ return utf8_wordwrap($string, $length, $break, $cut);
}
-function utf8_wordwrap($str,$len,$what){
- # usage: utf8_wordwrap("text",3,"<br>");
- # by tjomi4`, thanks to SiMM.
- # www.yeap.lv
- $from=0;
- $str_length = preg_match_all('/[\x00-\x7F\xC0-\xFD]/', $str, $var_empty);
- $while_what = $str_length / $len;
- while($i <= round($while_what)){
- $string = preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
- '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
- '$1',$str);
- $total .= $string.$what;
- $from = $from+$len;
- $i++;
- }
- return $total;
+/**
+ * wordwrap for utf8 encoded strings, http://milianw.de/section:Snippets/content:UTF-8-Wordwrap
+ *
+ * @param string $str
+ * @param integer $len
+ * @param string $what
+ * @return string
+ */
+
+function utf8_wordwrap($str, $width, $break,$cut = false){
+ if(!$cut){
+ $regexp = '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){'.$width.',}\b#U';
+ } else {
+ $regexp = '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){'.$width.'}#';
+ }
+ if(function_exists('mb_strlen')){
+ $str_len = mb_strlen($str,'UTF-8');
+ } else {
+ $str_len = preg_match_all('/[\x00-\x7F\xC0-\xFD]/', $str, $var_empty);
+ }
+ $while_what = ceil($str_len / $width);
+ $i = 1;
+ $return = '';
+ while ($i < $while_what){
+ preg_match($regexp, $str,$matches);
+ $string = $matches[0];
+ $return .= $string . $break;
+ $str = substr($str,strlen($string));
+ $i++;
+ }
+ return $return.$str;
}
?>
\ No newline at end of file
More information about the pLog-svn
mailing list