Home > Backend Development > PHP Tutorial > Use regular expressions to match multiple results in PHP and randomly replace one of the results_PHP Tutorial

Use regular expressions to match multiple results in PHP and randomly replace one of the results_PHP Tutorial

WBOY
Release: 2016-07-14 10:09:16
Original
1023 people have browsed it

Use regular matching characters. If it is very simple to replace all, just use preg_replace. But now I need to randomly replace one of the multiple successful matching results, which is a bit troublesome. I wrote a function myself to solve it. I don’t know if there is any other better way. Example "I have a dream. I have a dream. I have a dream. I have a dream." Matches the expression '/i/'. There are 4 matching results in the above string, and I just need to randomly replace one of them. Replace i with hell.

My code is as follows:
[php]
//Regular processing function
function rand_replace_callback($matches) {
global $g_rand_replace_num, $g_rand_replace_index, $g_rand_replace_str;
$g_rand_replace_index++;
if($g_rand_replace_num==$g_rand_replace_index){
return $g_rand_replace_str;
}else {
return $matches[0];
}                                                                            
}
//Random regular replacement function If there are multiple matching units, randomly replace one of them.
//Pay attention to global $g_rand_replace_num, $g_rand_replace_index, $g_rand_replace_str; these three global variables should not conflict with other ones
//Depends on a regular processing function to record the total number of matching units, take a random value within the range of the total number, and process it if it is judged to be equal in the regular processing function.
function rand_preg_replace($pattern, $t_g_rand_replace_str, $string)
{
global $g_rand_replace_num, $g_rand_replace_index, $g_rand_replace_str;
preg_match_all($pattern, $string, $out);$find_count = count($out[0]); //Total number of matching units
$g_rand_replace_num = mt_rand(1, $find_count); //A collection that meets regular search conditions
$g_rand_replace_index = 0; //index during the actual replacement process
$g_rand_replace_str = $t_g_rand_replace_str;
echo "Now there are {$find_count} matches found
";
$ss=preg_replace_callback($pattern,"rand_replace_callback",$string);
return $ss; www.2cto.com
}
$string = "I have a dream. I have a dream. I have a dream. I have a dream.";
echo rand_preg_replace('/I/', "hell", $string);
Expanding thinking, I want to reduce the concept of the first result being replaced. What should I do? There are cases where it's not good to be the first to be replaced, and only a small number of results are needed to be the first to be replaced.

http://www.bkjia.com/PHPjc/477700.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477700.htmlTechArticleUse regular matching characters. If it is very simple to replace all, just use preg_replace. But now I want to randomly replace one of the multiple successful matching results, this...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template