用php中的perg_replace函数将一行替换为一个大字符串变量
我有两个变量用php中的perg_replace函数将一行替换为一个大字符串变量,php,Php,我有两个变量$bigStr和$linesToReplace。我希望,如果有任何行同时在(数组)$linesToReplace和(字符串)$bigStr中匹配,则将其替换为$bigStr,并高亮显示 到目前为止,我所做的是: $bigStr = preg_replace('/'.implode('|', $linesToReplace).'/i', '<span style="color:red;"><b>$0</b></span>', $bigSt
$bigStr
和$linesToReplace
。我希望,如果有任何行同时在(数组)$linesToReplace
和(字符串)$bigStr
中匹配,则将其替换为$bigStr
,并高亮显示
到目前为止,我所做的是:
$bigStr = preg_replace('/'.implode('|', $linesToReplace).'/i', '<span style="color:red;"><b>$0</b></span>', $bigStr);
$bigStr=preg_replace('/'.intlode('|',$linesToReplace)。'/i','0',$bigStr);
但它只适用于一个单词,而不是一整行。您需要的是这些方面的东西
$bigStr = 'Hello foo and bar and baz.';
$linesToReplace = array('/foo/','/bar/','/baz/');
$bigStr = preg_replace($linesToReplace, '<span style="color:red;"><b>$0</b></span>', $bigStr);
$bigStr='Hello foo and bar and baz';
$linesToReplace=array(“/foo/”、“/bar/”、“/baz/”);
$bigStr=preg_replace($linesToReplace,$0',$bigStr);
这将产生以下内容:
确保将分隔符(/在本例中)添加到
$linesToReplace
数组中的每个元素中会产生一个区分大小写的问题