3

How to use php preg_replace and regular expression to remove all the hyperlink which contain <a href="#. Here is what I write, but it doesn't work

$newlink = preg_replace('/^<a href="#(.*)" (?:.*?)>(.*)<\/a>/is', '', $link);

I want replace these links which as aa anchor mark

<a href="#part1">go to part1</a>
<a href="#part2">go to part2</a>
<a href="#part3">go to part3</a>

to empty value.

2
  • They do not match, because they don't have a space after the second quote mark " as required by your regex. But please be very careful if you handle HTML with regular expressions. In almost any case it is not what you want to do but use a proper parser instead. Commented Jul 1, 2011 at 17:20
  • Where do you get the input from? I think using an HTML parse and XPath will be easier. Commented Jul 1, 2011 at 17:21

5 Answers 5

3

Let me start by saying that using regular expressions for parsing/modifying HTML documents can be the wrong approach. I'd encourage you to check out DOM Document if you're doing this for any other modifications.

With that said, making your expression non-greedy (.*?) will probably work.

$newlink = preg_replace('/^<a href="#(.*?)"[^>]+>(.*?)<\/a>/', '', $link);

Note: This also assumes href is the first attribute in all you anchor tags. Which may be a poor assumption.

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

Comments

1

It's remove only href attribute from html

echo preg_replace('/(<[^>]+) href=".*?"/i', '$1', $content);

Comments

0

If you want to replace only the href's value, you'll need to use assertions to match it without matching anything else. This regex will only match the URL:

/(?<=<a href=")#.*?(?=")/

Comments

0

Thanks two of all, But both of your answer still not work for me.

I am not good at regular expression, I read many documents and write again. maybe this looks ugly, but it work :)

$newlink = preg_replace('/(<a href=")#.*?(<\/a>)/is', '', $link);

1 Comment

No need for the capture groups. The regex can be simplified to just: '/<a href="#.*?<\/a>/is'
0

use /(<a.*?href=([\'"]))(.*?)(\2.*?>)/i regex

<?php 
     $oldLink = '<a href="http://test.com">click this link</a>';
     $res = "http://stackoverflow.com";
     echo $newLink = preg_replace('/(<a.*?href=([\'"]))(.*?)(\2.*?>)/i', '$1'.$res.'$2', $oldLink);    
?>

if you wants to test this regex, then you can here https://regex101.com/r/mT1sVK/1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.