I have many links out of one foreach. each foreach output some dom tree like:
<span id="span1">
<a(.*?)/test/(.*?)>word1</a>
</span>
<span id="span2">
<a(.*?)/fold/(.*?)>word2</a>
</span>
Now I want to replace the last link of the two, change the whole code as:
<span id="span1">
<a(.*?)/test/(.*?)>word1</a><!-- remain this link, do not replace. -->
</span>
<span id="span2">
<a href="#" class="replaced" title="word2">word2</a>
</span>
My preg_replace code here:
$code = '<span><a href="http://localhost/test/index.html">test1</a></span><span><a href="http://localhost/fold/index.html">test2</a></span>';
echo preg_replace('%href="(.*?)/fold/(.*?)"%', 'href="#" class="replaced" title="$2"', $code);
I want get code like
<span id="span1">
<a href="http://localhost/test/index.html">test1</a>
</span>
<span id="span2">
<a href="#" class="replaced" title="test2">test2</a>
</span>
But it will output <span id="span1"><a href="#" class="replaced" title="index.html">word2</a></span>
, not as I expected. how to do well? thanks.