Reputation: 10643
I'm trying to add a span after all opening anchor tags no matter what is inside the anchor. I'm working in a CMS so $content
will hold all my links and is a long HTML string.
I first tried str_replace()
but it apparently doesn't use wildcards.
$content = str_replace( '<a href="%">', '<a href="%"><span class="fa"></span>', $content );
So then I tried preg_replace()
but I not only do not know enough regex to do it, but I don't necessarily replace the things I find but instead find what I want to find then append the <span>
. What is a good function to Find a my beginning anchors then append a <span>
or any other data after it?
Upvotes: 0
Views: 722
Reputation: 2481
Use preg_replace
$content = preg_replace('/<a (.*?)>(.*?)<\/a>/', '<a $1 ><span class="fa"></span>$2</a>', $content);
(.*?)
is get anything, then the $1
corresponds to replacing the first (.*?)
back in. Therefore the second occurrence is then placed where $2
is.
Upvotes: 3