我想获取preg_replace如果替换,则显示结果,否则为空白
<?php
$text = "text4 text1 text2 text3";
$replace = [
'/\btext1\b/' => 'replace1', //replace case sensitive
'/\btext2\b/i' => 'replace2', //non-sensitive
'/\b(text3|text4)\b/i' => 'replace3', //replace both text3 and text4
];
$result = preg_replace(array_keys($replace), $replace, $text);
echo $result;
结果= replace3 replace1 replace2 replace3
还有
<?php
$text = "text4x text1x text2x text3x";
$replace = [
'/\btext1\b/' => 'replace1', //replace case sensitive
'/\btext2\b/i' => 'replace2', //non-sensitive
'/\b(text3|text4)\b/i' => 'replace3', //replace both text3 and text4
];
$result = preg_replace(array_keys($replace), $replace, $text);
echo $result;
结果= text4x text1x text2x text3x
如果不替换任何内容,则显示原始$ text,如果任何单词不替换,我想在此处显示空白结果
答案 0 :(得分:0)
您可以使用strcmp比较字符串。
strcmp($ text,$ result); //如果字符串相同则返回0
$compare = strcmp($text,$result);
if($compare == 0){ echo $text; }
else { echo 'Result not found'; }