Matt
Matt

Reputation: 1

Preg_replace: How to strip the hyperlinks, but only if the hyperlink contains particular text

I want to strip away hyperlinks, so only the text remains. So that

<a href='www.mysite.com/directory/'>Click Here For My Site</a>

Simply becomes

Click Here For My Site

I have this so far and it works:

$content = preg_replace('#<a.*>(.*)</a>#isU', "$1",  $content);

HOWEVER, I only want to strip the hyperlink away IF the hyperlink contains the specific test 'directory' in it.

In that case, the link below, which does not contain the word 'directory', would NOT be stripped of it's hyperlink.

<a href='www.mysite.com/FORUM/'>Click Here For My Site</a>

I cannot figure out how to do this! Whatever I try doesn't seem to work... :(

Upvotes: 0

Views: 494

Answers (1)

Geoffrey Wagner
Geoffrey Wagner

Reputation: 818

$content = preg_replace('/<a[^>]+directory[^>]*>(.*)<\/a>/isU', "$1",  $content);

This will grab the text in the hyperlink as long as there id the word "Directory" in the opening <a> tag

Upvotes: 1

Related Questions