note 105473 modified in function.mb-eregi-replace by danbrown

From: Date: Mon, 22 Aug 2011 14:01:08 +0000
Subject: note 105473 modified in function.mb-eregi-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182640@lists.php.net to get a copy of this message
when trying to find a way to strip newline from a multibyte UTF-8 string i got to this function just to discover later that POSIX don't "do" newline so i can't strip them, examples of what i tried are : \r\n , \\r\\n , (\\r\\n) (\\r|\\n) and got no result so since i wanted something like mb_nl2br() that's simple i wrote this little recursive function for UTF-8: <?php function mb_str_replace($find,$replace,&$str) { $i = mb_strpos($str,$find, 0,"UTF-8"); if ($index===false) {return;} $str = mb_substr($str, 0,$i).$replace.mb_substr($str, $i+mb_strlen($find,"UTF-8"),mb_strlen($str,"UTF-8")); $this->mb_str_replace($find,$replace,$str); } ?> note: moderate unit tesing was done, changed to other encodings --was-- when trying to find a way to strip newline from a multibyte UTF-8 string i got to this function just to discover later that POSIX don't "do" newline so i can't strip them, examples of what i tried are : \r\n , \\r\\n , (\\r\\n) (\\r|\\n) and got no result so since i wanted something like mb_nl2br() that's simple i wrote this little recursive function for UTF-8: function mb_str_replace($find,$replace,&$str) { $i = mb_strpos($str,$find, 0,"UTF-8"); if ($index===false) {return;} $str = mb_substr($str, 0,$i).$replace.mb_substr($str, $i+mb_strlen($find,"UTF-8"),mb_strlen($str,"UTF-8")); $this->mb_str_replace($find,$replace,$str); } note: moderate unit tesing was done, changed to other encodings http://php.net/manual/en/function.mb-eregi-replace.php

« previous php.notes (#182640) next »