2

I'm not sure how to explain this, so I'll show it on my code.

<a href="link.php">First</a> and 
<a href="link.php" class="delete">Second</a> and 
<a href="link.php">Third</a>

how can I delete opening <a href="link.php" class="delete"> and closing </a> but not the rest?

I'm asking for preg_replace(); and I'm not looking for DomDocument or others methods to do it. I just want to see example on preg_replace();

how is it achievable?

genesis
  • 50,477
  • 20
  • 96
  • 125
  • provided there is no more HTML inside the `..` element, then it's relatively simple. If there is (or if there could be) any sub elements, then it becomes much more difficult. – Spudley Jul 21 '11 at 15:04
  • 5
    You only want the _wrong_ solution? Not interested. – Lightness Races in Orbit Jul 21 '11 at 15:05
  • Okay, @Tomalak, show me example with ANY method to achive this – genesis Jul 21 '11 at 15:06
  • 1
    Are you trying to disable links to the current page? If you explain what you *actually* want, you will get better solutions than asking how to do what you already decided was best. I'm sure there is some reason for this, and it can be achieved without a regular expression. – Wesley Murch Jul 21 '11 at 15:07
  • I can understand using `preg_replace()` for such a trivial thing. But it is not the appropriate solution. Albeit unlikely, if there were nested anchor tag this would fail. That's one of the reasons Regular Expressions – Jason McCreary Jul 21 '11 at 15:08
  • @webbiedave: So let's teach him poor habits? – Lightness Races in Orbit Jul 21 '11 at 15:10
  • @Tomalak Geret'kal: Comment was targeted at the other Tomalak. I just edited the comment to clarify. – webbiedave Jul 21 '11 at 15:11
  • @webbiedave: Thanks. :) My response stands, though... – Lightness Races in Orbit Jul 21 '11 at 15:11
  • @Tomalak: Can you tell me, what's wrong with preg_replacing html? Is there some real answer WHY is it bad idea? – genesis Jul 21 '11 at 15:12
  • @webbiedave: Thank you for support. However I have to tell that this is very begginer question though. I have to think about it little bit more before asking new question. Sure I know solution for this, but someone had to remind me :/ – genesis Jul 21 '11 at 15:13
  • @genesis: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Lightness Races in Orbit Jul 21 '11 at 15:15
  • @genesis: The answer is in my first comment, really. I'm not berating you, I'm just being direct, so no offense. *"How can I parse/modify HTML with regex and why is it such a bad idea?"* is a topic that has been discussed on SO *to no end* and I won't start repeating the arguments here. – Tomalak Jul 21 '11 at 15:17
  • @Tomalak:Okay. I knew answer for my own question for sure, but I didn't about such an easy answer like hakre given. – genesis Jul 21 '11 at 15:19

5 Answers5

7

Only pick the groups you want to preserve:

$pattern = '~(<a href="[^"]*" class="delete">)([^<]*)(</a>)~';
//                   1                           2      3
$result = preg_replace($pattern, '$2', $subject);

You find more examples on the preg_replace manual page.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • @genesis: You wanted to delete links with class `"delete"`? No? This code does the opposite. – Lightness Races in Orbit Jul 21 '11 at 15:09
  • @hakre: [Oh, really](http://www.ideone.com/oGoBM)? I suggest you try out your code before you post it. – Lightness Races in Orbit Jul 21 '11 at 15:11
  • @Tomalak: I've read the question that the text should be preserved, but the link removed. Naturally this only works on strings that match the regex, so no parser here. – hakre Jul 21 '11 at 15:12
  • @Tomalak: Was just using XMLReader for parsing [lately for an answer](http://stackoverflow.com/questions/6775785/how-can-i-edit-my-code-to-echo-the-data-of-childs-element-where-my-search-term-w/6777403#6777403). – hakre Jul 21 '11 at 15:17
5

Since you asked me in the comments to show any method of doing this, here it is.

$html =<<<HTML
<a href="link.php">First</a> and 
<a href="link.php" class="delete">Second</a> and 
<a href="link.php">Third</a>
HTML;

$dom = new DOMDocument();
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);

$elems = $xpath->query("//a[@class='delete']");
foreach ($elems as $elem) {
    $elem->parentNode->removeChild($elem);
}

echo $dom->saveHTML();

Note that saveHTML() saves a complete document even if you only parsed a fragment.

As of PHP 5.3.6 you can add a $node parameter to specify the fragment it should return - something like $xpath->query("/*/body")[0] would work.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
4
$pattern = '/<a (.*?)href=[\"\'](.*?)\/\/(.*?)[\"\'](.*?)>(.*?)<\/a>/i';
$new_content = preg_replace($pattern, '$5', $content);
Devan
  • 117
  • 5
1
$pattern = '/<a[^<>]*?class="delete"[^<>]*?>(.*?)<\/a>/';

$test = '<a href="link.php">First</a> and <a href="url2.html" class="delete">Second</a> and <a href="link.php">Third</a>';
echo preg_replace($pattern, '$1', $test)."\n";

$test = '<a href="link.php">First</a> and <a href="url2.html"><b class="delete">seriously</b></a> and <a href="link.php">Third</a>';
echo preg_replace($pattern, '$1', $test)."\n";

$test = '<a href="link.php">First</a> and <a href="url2.html" class="delete"><b class="delete">seriously</b></a> and <a href="link.php">Third</a>';
echo preg_replace($pattern, '$1', $test)."\n";

$test = '<a href="link.php">First</a> and <a  class="delete" href="url2.html">Second</a> and <a href="link.php">Third</a>';
echo preg_replace($pattern, '$1', $test)."\n";
guido
  • 18,864
  • 6
  • 70
  • 95
0
preg_replace('@<a href="[^"]+" class="delete">(.+?)</a>@', '$1', $html_string);

It is important to understand this is not an ideal solution. First, it requires markup in this exact format. Second, if there were, say, a nested anchor tag (albeit unlikely) this would fail. These are some of the many reasons why Regular Expressions should not be used for parsing/manipulating HTML.

Jason McCreary
  • 71,546
  • 23
  • 135
  • 174