Question

i wanna to add strong tag before some --images-- tag in this text which --images-- tag
have not strong tag above of them,Here is a piece of code I wrote but obviously the foreach is not correct,which all --images-- will be replace, I want to replace the third --images-- tag to strong , and the previous two remain unchanged .

$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();

the result i wanna is :

<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>
Was it helpful?

Solution

you are most likely looking for preg_replace_callback() as preg_match_all does only match and not replace.

However if you want to do it with preg_match_all you need to capture match offsets (PREG_OFFSET_CAPTURE constant), slice the original buffer to non-matching regions and matching regions and then handle all the matching regions your own while preserving the non-matching regions. after you're happy with your region changes you can merge them together into the result buffer.

As you can see, preg_replace_callback might be easier to use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top