note 117314 deleted from function.preg-replace by cmb

From: Date: Sun, 22 Sep 2019 08:01:14 +0000
Subject: note 117314 deleted from function.preg-replace by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-212306@lists.php.net to get a copy of this message
Note Submitter: safranil (at) safranil [dot] fr ---- Warning, preg_replace() has an unexpected behaviour on UTF-8 strings when you use an empty regular expression like "/()/" ! If you build your regular expression in PHP like this : $words = array(); foreach (explode(" ", $what) as $w) if (mb_strlen($w) > 0) $words[] = preg_quote($w, "/"); return preg_replace('/(' . implode("|",$words) . ')/iu', '<span class="text-maroon">\\1</span>', $text); Always check if $words array isn't empty : if (count($words) == 0) return $text;

« previous php.notes (#212306) next »