我有这样的字符串:
$str = '<div> text echo <div class="code">echo "test!";</div> </div>';
如何将echo
替换为font
代码?
例如on out必须是字符串,如下所示:
$str = '<div> text echo <div class="code"><font color="green">echo</font> "test!";</div> </div>';
我试过
$str = preg_replace("/<div class='code'>(echo)<\/div>/Usi",'<font color="green">echo</font>',$str);
但没有意义,所以我怎么做呢?
更新所以这似乎是解决方案:
$str = '<div> text echo <div class="code">echo "test!"; echo "very test!";</div> </div>';
/*$str = preg_replace('/(<div class="code">.*?)(echo)(.*?<\/div>)/si',
'$1<font color="green">$2</font>$3', $str);*/
$str = preg_replace_callback(
'/(<div class="code")(.*?)(<\/div>)/si',
function ($matches) {
return $matches[1].str_replace('echo','<font color="green">echo</font>',$matches[2]).$matches[3];
},
$str);
echo $str;
答案 0 :(得分:1)
$str = preg_replace('/(<div class="code">.*?)(echo)(.*?<\/div>)/si',
'$1<font color="green">$2</font>$3', $str);
答案 1 :(得分:1)
啊,为了应对多次替换,可以使用preg_replace_callback。
$str = preg_replace_callback(
'/(<div class="code")(.*?)(<\/div>)/si',
function ($matches) {
return $matches[1].str_replace('echo','<font color="green">echo</font>',$matches[2]).$matches[3];
},
$str);
内部'功能',可以在$ match [2]上执行多次替换。那只是代码块中的代码。
(如果嵌套在代码div中,它仍然无法工作!)
答案 2 :(得分:0)
你的报价是假的,
preg_replace("/<div class='code'>(echo)<\/div>/Usi",
也是:
preg_replace('/<div class="code">(echo)<\/div>/Usi',
你的常规也是假的,也许可以测试一下:
$str = '<div> text echo <div class="code">echo "test!";</div> </div>';
$match = preg_replace('/<div class="code">echo(\s*.*)<\/div>/Usi','<div class="code"><font color="green">echo</font>'."\\1".'</div>',$str);
echo $str;
也许您可以使用str_replace函数,例如
$str = '<div> text echo <div class="code">echo "test!";</div> </div>';
$search = '<div class="code">echo';
$replace = '<div class="code"><font color="green">echo</font>';
$str = str_replace($search, $replace, $str);
echo $str;
答案 3 :(得分:0)
$str = '<div> text echo <div class="code">echo "test!";echo "test!";</div> </div>';
// extract the content in the <div class="code">
preg_match('/<div class="code">(.*)<\/div>/Usi',$str, $matches);
// $matches[1] is the content and excute replace singly
$result = str_replace('echo', '<font color="green">echo</font>', $matches[1]);
// let the replace result insert the origin str
$str = preg_replace('/<div class="code">(.*)<\/div>/Usi', '<div class="code">'.$result.'</div>', $str);
echo $str;
答案 4 :(得分:0)
也许这是你的答案:
$str = '<div> text echo <div class="code">echo "test!";echo "test!";</div> </div><br><div> text echo <div class="code">echo "test!";echo "test!";</div> </div>';
// extract the content in the <div class="code">
preg_match_all('/(<div class="code">.*<\/div>)/Usi',$str, $matches);
$i = 0;
$newStr = '';
$arr = preg_split('/<div class="code">(.*)<\/div>/Usi', $str);
foreach($matches as $match) {
// $matches[1] is the content and excute replace singly
$result = str_replace('echo', '<font color="green">echo</font>', $match[1]);
$newStr .= $arr[$i].$result;
$i++;
}
// add the end of the str
$newStr = $newStr.$arr[$i];
echo $newStr;