0

I have the following string that is part of a larger string which I think I'll need to perform a preg_replace on.

&nbsp;>&nbsp;<a href=''>About Us</a>

The larger string is:

<a href='http://ecolution.dev'>Home</a> &nbsp;>&nbsp;<a href=''>About Us</a> &nbsp;>&nbsp;Meet the Directors

Now I can match the first string easily with something like /&nbsp;.*?<\/a>\s+/ - that works fine.

What I actually need to do is remove the &nbsp;>&nbsp;<a href=''> from the string and also the </a>. So that plain text is left in the larger string.

I could remove the 2 parts separately in multiple preg_replace calls but that doesn't seem like the best option to me at all.

Is there a way to get any text between &nbsp;>&nbsp;<a href=''> and </a> and then output it as plain text so that &nbsp;>&nbsp;<a href=''>About Us</a> simply becomes About Us?

EDIT

I should have mentioned this earlier. This is a dynamically created breadcrumb system inside ExpressionEngine. Some entries have empty href, so <a href=''>abc</a> and those entries need to have their a tags removed, hence why trying to match the characters/strings above so that it's just plain text

<a href='http://ecolution.dev'>Home</a> &nbsp;>&nbsp;<a href=''>About Us</a> &nbsp;>&nbsp;Meet the Directors

would become

<a href='http://ecolution.dev'>Home</a> &nbsp;>&nbsp;About Us &nbsp;>&nbsp;Meet the Directors

3
  • 1
    Use htmlspecialchars_decode() to convert those &nbsp; and then use some HTML parser. Have fun. Commented Mar 11, 2016 at 10:19
  • Possible duplicate of Preg match text in php between html tags Commented Mar 11, 2016 at 10:26
  • @Egg not so sure as this string is populated dynamically. Let me edit to add more info to my question Commented Mar 11, 2016 at 10:31

1 Answer 1

2

This will remove the tags for any anchor link with an empty href: <a href=''>text</a>:

$html = "<a href='http://ecolution.dev'>Home</a> &nbsp;>&nbsp;<a href=''>About Us</a> &nbsp;>&nbsp;Meet the Directors";
$result = preg_replace("/<a href=''>(.*?)<\/a>/", "$1", $html);
// Result = "<a href='http://ecolution.dev'>Home</a> &nbsp;>&nbsp;About Us &nbsp;>&nbsp;Meet the Directors"

Using '$1' in the second parameter of preg_replace allows us to put back in the first matched string (from our first parameter of preg_replace).

Sign up to request clarification or add additional context in comments.

3 Comments

So this won't actually remove the a tags, this will just extract the text? I'd need to replace the string that is wrapped in the empty a tag with the string extracted
I'm confused. You asked "is there a way to get any text and then output it as plain text so that &nbsp;>&nbsp;<a href=''>About Us</a> simply becomes About Us. With my example, you get that in $match[2], so $html = $match[2] if you want to replace the string with the text.
That's probably me badly wording my question, while you are right, I would need to replace it in the strings place, not extract it and use it elsewhere. Let me update the question properly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.