如何更改一些与preg_match_all匹配的元素?

时间:2014-05-08 09:23:32

标签: php preg-replace str-replace preg-match-all

我想在本文中的--images--标签之前添加强标签--images--标签
它们上面没有强大的标签,这是我写的一段代码,但显然foreach不正确,所有--images--都将被替换,我想将第三个--images--标签替换为strong,前两个保持不变。

$text = <<<EOT
<p class="paragraph">“How do you make peace with that?” I said.</p>
<!--images-->
<p class="paragraph">He shrugged, laughed bitterly and answered, “I’m hoping to leave them a lot of money.”</p>
<p class="paragraph">The American dream, 2014 edition: Squirrel away nuts for a leaner tomorrow. The worst is yet to come, so insure yourself against it if you’re among the lucky few who can.</p>
<strong>i am a strong tag</strong>
<p class="paragraph">I was reminded of my conversation with him when I read last week about a fresh projection, from a branch of the World Bank, that the <a href="http://www.cnbc.com/id/101626562 ">Chinese economy might overtake ours</a> by the end of this year, finishing our century-plus reign as the world’s wealthiest nation. What a run we had! It was great while it lasted.</p>
<!--images-->
<p class="paragraph">And it will probably last much longer than another few months. The projection relied on disputed arithmetic. These matters aren’t neat and clean.</p>
<!--images-->
<p class="paragraph">But our slide to No. 2 nonetheless seems inevitable, so much so that most Americans think it has <em>already</em> happened. For the last six years, when the Gallup Poll asked them which country was the world’s “leading economic power,” <a href="http://www.gallup.com/poll/167498/americans-view-china-mostly-unfavorably.aspx">more answered China</a> than said the United States. This year, the spread was an astonishing 52 to 31 percent. Fewer than one in three Americans puts us on top, even though we actually remain there.</p>
EOT;

preg_match_all('/(?<=<!--images-->)(?:(?!strong)[\s\S])*?(<!--images-->)/i', $text, $images);

if (!empty($images[1]))
{
    foreach ($images[1] as $match)
    {
        $text = str_replace($match, '<strong></strong><!--images-->', $text);
    }
}

var_dump( $text);exit();

我想要的结果是:

<p class="paragraph">“How do you make peace with that?” I said.</p>
<!--images-->
<p class="paragraph">He shrugged, laughed bitterly and answered, “I’m hoping to leave them a lot of money.”</p>
<p class="paragraph">The American dream, 2014 edition: Squirrel away nuts for a leaner tomorrow. The worst is yet to come, so insure yourself against it if you’re among the lucky few who can.</p>
<strong>i am a strong tag</strong>
<p class="paragraph">I was reminded of my conversation with him when I read last week about a fresh projection, from a branch of the World Bank, that the <a href="http://www.cnbc.com/id/101626562 ">Chinese economy might overtake ours</a> by the end of this year, finishing our century-plus reign as the world’s wealthiest nation. What a run we had! It was great while it lasted.</p>
<!--images-->
<p class="paragraph">And it will probably last much longer than another few months. The projection relied on disputed arithmetic. These matters aren’t neat and clean.</p>
<strong></strong><!--images-->
<p class="paragraph">But our slide to No. 2 nonetheless seems inevitable, so much so that most Americans think it has <em>already</em> happened. For the last six years, when the Gallup Poll asked them which country was the world’s “leading economic power,” <a href="http://www.gallup.com/poll/167498/americans-view-china-mostly-unfavorably.aspx">more answered China</a> than said the United States. This year, the spread was an astonishing 52 to 31 percent. Fewer than one in three Americans puts us on top, even though we actually remain there.</p>

1 个答案:

答案 0 :(得分:0)

您最有可能寻找preg_replace_callback(),因为preg_match_all仅匹配而非替换。

但是 if 你希望用preg_match_all来实现它,你需要捕获匹配偏移量(PREG_OFFSET_CAPTURE常量),将原始缓冲区切片到不匹配的区域和匹配区域然后处理您自己的所有匹配区域,同时保留不匹配的区域。在对区域更改感到满意后,可以将它们合并到结果缓冲区中。

如您所见,preg_replace_callback可能更容易使用。