Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

isaacongoma's avatar

How can I remove a link from php

am having a text that contains several urls. But I want to strip of specific url and keep others. Considering the text below, how can I remove the url "https://opportunitydesk.org/2022/03/16/marilyn-stafford-fotoreportage-award-2022/" from the text and maintain the rest? Am using laravel 8.

Consider this text

<p>Applications are open for the <a href="https://opportunitydesk.org/2022/03/16/marilyn-stafford-fotoreportage-award-2022/">Marilyn Stafford FotoReportage Award 2022</a>.  The Award, facilitated by FotoDocument and supported by Nikon, is granted annually to a professional women photographer towards the completion of a compelling and cohesive documentary photo essay which addresses an important social, environmental, economic or cultural issue, whether local or global.</p>

  <p><strong><a href="https://fotodocument.org/wp-content/uploads/MSFA-APPLICATION-FORM-2022.docx" target="_blank" rel="noreferrer noopener nofollow">Click here to download the application form</a></strong></p>
<p>For more information, visit <strong><a href="https://fotodocument.org/fotoaward/" target="_blank" rel="noreferrer noopener nofollow">FotoReportage Award</a>.</strong></p>

0 likes
17 replies
Sinnbeck's avatar

What are the rules for which urls to remove/keep?

1 like
Sinnbeck's avatar

@isaacongoma something like

$replaced = preg_replace('/("https:\/\/opportunitydesk\.org[\/a-zA-Z0-9-]+")/', '"#"', $string);
isaacongoma's avatar

@Sinnbeck Hi, am currently getting this which is still okay but is it possible to remove the "a" tag associated with the url with the "href" too but still keep the text in it?

<p>Applications for the <a href="#">Vilcek Prizes for Creative Promise in Music 2022-2023</a> are now open.

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@isaacongoma Something like

$replaced = preg_replace('/(<a.*https:\/\/opportunitydesk\.org.*<\/a>)/', '', $string);
isaacongoma's avatar

@Sinnbeck Thanks, though the text in the "a" tag got removed too. Is it possible to retain the text? Here is the ouput

<p>Applications for the  are now open. The Profeta Entrepreneur-In-Residence (EIR) program provides training, access, and support to black, indigenous, and people of color who are interested in becoming high-tech, high-growth entrepreneurs.</p>
Sinnbeck's avatar

@isaacongoma

$replaced = preg_replace('/(<a.*https:\/\/opportunitydesk\.org.*>(.*)<\/a>)/', '$2', $string);
kokoshneta's avatar

@Sinnbeck Note that this will do a greedy search, so sequential links will get borked; e.g., if you have this input string:

<p>Applications <a href="/someotherlink">are open</a> for the <a href="https://opportunitydesk.org/2022/03/16/marilyn-stafford-fotoreportage-award-2022/">Marilyn Stafford FotoReportage Award 2022</a>.  The Award, facilitated by FotoDocument and supported by Nikon, is granted annually to a professional women photographer towards the completion of a compelling and cohesive documentary photo essay which addresses an important social, environmental, economic or cultural issue, whether local or global.</p>

Then the output will be this:

Applications Marilyn Stafford FotoReportage Award 2022. The Award, facilitated by FotoDocument and supported by Nikon, is granted annually to a professional women photographer towards the completion of a compelling and cohesive documentary photo essay which addresses an important social, environmental, economic or cultural issue, whether local or global.

Note the beginning, which should say “applications are open for the Marilyn Stafford FotoReportage Award 2022.

Making it non-greedy should fix that, but PHPTester seems to be stripping some characters when I try, and I can’t be bothered making a proper test case, so this workaround should work as well:

$replaced = preg_replace('/(<a[^>]*https:\/\/opportunitydesk\.org.*>(.*)<\/a>)/', '', $string);

That is, changing a.*https (“a, followed by any character any number of times, followed by https”) to a[^>]*https (“a, followed by any character that’s not > any number of times, followed by https”).

1 like
Sinnbeck's avatar

@kokoshneta Good point. Was just quickly thrown together and only tested on the posted example :)

1 like
Snapey's avatar

if it's an exact string, then use str_replace

1 like

Please or to participate in this conversation.