Str_replace can find replacement regular characters,
Preg_replace to find replacement carriage return newline characters (\ r \ n)
Preg_replace is commonly used in
The code is as follows |
Copy Code |
<meta name= "description" content= <?php echo htmlspecialchars (utf_substr (' preg_replace ', ', ', '/\r\n/ Replace (', ', Strip_tags ($this->getdescription)))?> ">
|
Content filtering, filter carriage return line (\ r \ n), etc.
Put
The code is as follows |
Copy Code |
<a href= "/%E5%8A%A8%E4%BD%9C%E5%86%92%E9%99%A9_1.HTML1" > Home </a> <a href= "/%e5%8a%a8%e4%bd%9c%e5%86% 92%e9%99%a9_1.html0 "class=" A1 "> Prev </a> |
Replace with the Str_replace function and the Preg_replace function
The code is as follows |
Copy Code |
<a href= "/%e5%8a%a8%e4%bd%9c%e5%86%92%e9%99%a9_1.html" > Home </a> <a href= "/%e5%8a%a8%e4%bd%9c%e5%86% 92%e9%99%a9_1.html "class=" A1 "> Prev </a> |
Example
The code is as follows |
Copy Code |
$pages = $keyword _data_db->pages; $pages = Str_replace (' page= ', ', $pages); $pages = Preg_replace ('/_ (0-9]+). HTML ([0-9]+)/', ' _$2.html ', $pages); $pages = Str_replace (' _0.html ', ' _1.html ', $pages); |
Let's look at a comparative example.
The code is as follows |
Copy Code |
$str = ' 111111110000000000000000000000000000000111000001000100010000010010000010010000010100000010 '; $str = Str_repeat ($str, 1); $pattern 1 = Array (' 12345 ' => ', ' 67891 ' => '); $pattern 2 = Array (' A ' => ', ' 1234567890 ' => '); $pattern 3 = '/12345|67891/'; $pattern 4 = '/a|1234567890/'; $pattern 5 = Array (' 12345 ', ' 67891 '); $pattern 6 = Array (' A ', ' 1234567890 '); $t = Microtime (true); Echo Microtime (True)-$t, "/n"; 0.4768660068512 2.7257590293884 $t = Microtime (true); For ($i =0 $i <10000; $i + +) { Preg_replace ($pattern 3, ", $STR); } Echo Microtime (True)-$t, "/n"; 0.30504012107849 1.0864448547363 $t = Microtime (true); For ($i =0 $i <10000; $i + +) { Preg_replace ($pattern 4, ", $STR); } Echo Microtime (True)-$t, "/n"; 0.30298089981079 1.117014169693 $t = Microtime (true); For ($i =0 $i <10000; $i + +) { Str_replace ($pattern 5, ", $STR); } Echo Microtime (True)-$t, "/n"; 0.18029189109802 0.22510504722595 $t = Microtime (true); For ($i =0 $i <10000; $i + +) { Str_replace ($pattern 6, ", $STR); } Echo Microtime (True)-$t, "/n"; 0.18104100227356 0.23055601119995 Description: When the second parameter of Str_repeat is the output of the first digit at 1 o'clock, when the second digit is output for 8 o'clock |
Difference
The difference is that the content of the str_replace is fixed, determined, and of course you can use a variable, but the variable also represents a fixed, determined content, such as the complete replacement of all the \ n <br>.
The content of the Preg_replace (lookup) is described by rules, such as replacing all the content between < and > (HTML code). Of course preg_replace can also be used to replace fixed content.
According to the rules above, all str_replace can do preg_replace, but preg_replace is slower and more complicated to use, so we should try to use Str_replace.