0

我有这样的字符串:

$str = '<div> text echo <div class="code">echo "test!";</div> </div>';

如何echofont标签替换?

例如 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;
4

5 回答 5

1

Ah, to cope with multiple replaces, could use 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);

the inner 'function', can actully perform multiple replacements on $matches[2]. That will only be code inside a code block.

(not it still doesnt work if there are nested inside the code div!)

于 2013-09-23T14:12:14.037 回答
1
$str = preg_replace('/(<div class="code">.*?)(echo)(.*?<\/div>)/si',
       '$1<font color="green">$2</font>$3', $str);
  • 修正了引号
  • 允许回声两侧的文本(.*?)
  • 捕获之前/之后的文本,因此可以包含在替换中。
  • 在替换中使用 $2 而不是 echo ,因为您的正则表达式不区分大小写,它保持原始大小写。
  • 删除了 U,因为这使两次搜索变得不贪心。(但这主要是个人喜好)
于 2013-09-23T13:35:57.513 回答
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;
于 2013-09-23T14:38:05.850 回答
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;
于 2013-09-23T15:25:26.400 回答
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;
于 2013-09-23T14:03:23.157 回答