LatecomerX;10920372 wrote:...he preg equivalent for this would be:
$link = preg_replace('#<a href=("|\')?([^ "\'])("|\')>([<])</a>#i', '$4', $link);
Granted, this pattern can cause issues if there is an anchor tag as such:
<a class="whatever" href="http://www.whatever.com"><strong>link</strong></a>
Notice the snag(s)? With a pattern declaring <a href... this means that the href attribute has to come first, otherwise, that pattern will fail. Not only that, but there can only be the href attribute, followed by >
The second snag is ([<]*)</a>. Notice in my example the link text is surrounded in an html tag.. this too will cause the pattern to fail. My previous post escapes these issues altogether.. so long as it is an anchor tag with an href attribute, it will replace it.
Also, character classes are more efficient than alternations.. so something like ("|\')? creates issues of slower speeds (ok, admittedy we won't perceive the difference...), and creates an unecessary capture. Simply using ["\']? acheives the same thing, only slightly quicker and without the overhead of capuring. If using the alternation method without capturing, simply use (?:"|\')? instead (but the character class is still better suited in this case).