开发者

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

开发者 https://www.devze.com 2023-02-05 02:09 出处:网络
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>

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... :(


$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

0

精彩评论

暂无评论...
验证码 换一张
取 消