Not sure I really understand what you're asking, but if you :
- Have a string that contains some HTML
- and want to replace all links to abc.com by some text
Then, a good solution (better than regular expressions, should I say !) would be to use the DOM-related classes -- especially, you can take a look at the DOMDocument
class, and its loadHTML
method.
For example, considering that the HTML portion is declared in a variable :
$html = <<<HTML
<p>some text</p>
<a href="http://abc.com">Title</a>
<p>some more text</p>
<a href="http://xyz.com">Title</a>
<p>and some again</p>
HTML;
You could then use something like this :
$dom = new DOMDocument();
$dom->loadHTML($html);
$tags = $dom->getElementsByTagName('a');
for ($i = $tags->length - 1 ; $i > -1 ; $i--) {
$tag = $tags->item($i);
if ($tag->getAttribute('href') == 'http://abc.com') {
$replacement = $dom->createTextNode($tag->nodeValue);
$tag->parentNode->replaceChild($replacement, $tag);
}
}
echo $dom->saveHTML();
And this would get you the following portion of HTML, as output :
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<p>some text</p>
Title
<p>some more text</p>
<a href="http://xyz.com">Title</a>
<p>and some again</p>
</body></html>
Note that the whole <a href="http://abc.com">Title</a>
portion has been replaced by the text it contained.
If you want some other text instead, just use it where I used $tag->nodeValue
, which is the current content of the node that's being removed.
Unfortunately, yes, this generates a full HTML document, including the doctype declaration, <html>
and <body>
tags, ...