SBB
SBB

Reputation: 8970

PHP Remove specific URL from html string

I am trying to remove a specific URL from a text string. I have it working for the most part but I need to include both http or https versions of the link.

$link = '<a href="http://example.com/Tool/document.php?id=208" target="_BLANK">Document Requirementsd</a>';

$result = preg_replace('/<a href=\"http:\/\/' . $_SERVER["SERVER_NAME"] . '\/Tool\/document.php\?id=(.*?)\">(.*?)<\/a>/', "\\2", htmlspecialchars_decode($html));

Whats the best way to make sure that both http and https links are stripped?

Upvotes: 1

Views: 235

Answers (2)

Ajmal PraveeN
Ajmal PraveeN

Reputation: 413

So you need

$link = '&lt;a href=&quot;http://example.com/Tool/document.php?id=208&quot; target=&quot;_BLANK&quot;&gt;Document Requirementsd&lt;/a&gt;';
$link = '&lt;a href=&quot;https://example.com/Tool/document.php?id=208&quot; target=&quot;_BLANK&quot;&gt;Document Requirementsd&lt;/a&gt;';

Right?

You use get the https link by replace assuming http as static

//get primary link always http (we need to create http or https isset func)
$link = '&lt;a href=&quot;http://example.com/Tool/document.php?id=208&quot; target=&quot;_BLANK&quot;&gt;Document Requirementsd&lt;/a&gt;';
//making that as https
$https=str_replace('http','https',$link);
//So now you can do whatever you want.
$result = preg_replace('/<a href=\"http:\/\/' . $_SERVER["SERVER_NAME"] . '\/Tool\/document.php\?id=(.*?)\">(.*?)<\/a>/', "\\2", htmlspecialchars_decode($html));

If you want to check if the link is http or https, I can write some more code to find out.

Upvotes: 0

Aaron K.
Aaron K.

Reputation: 344

You can use (http|https):\/\/ to match both http or https!

Upvotes: 5

Related Questions