I have :
<a href="http://abc.com">Title</a>
And :
<a href="http://xyz.com">Title</a>
I want to replace link to text "Title", but only from http://abc.com. But I don't know how ( I tried Google ), can you explain for me. I'm not good in PHP.
Thanks in advance.
Not sure I really understand what you're asking, but if you :
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, ...
To cover another interpreted case:
$string = '<a href="http://abc.com">Title</a> <a href="http://xyz.com">Title</a>';
$pattern = '/\<\s?a\shref[\s="\']+([^\'"]+)["\']\>([^\<]+)[^\>]+\>/';
$result = preg_replace_callback($pattern, 'replaceLinkValueSelectively', $string);
function replaceLinkValueSelectively($matches)
{
list($link, $URL, $value) = $matches;
switch ($URL)
{
case 'http://abc.com':
$newValue = 'New Title';
break;
default:
return $link;
}
return str_replace($value, $newValue, $link);
}
echo $result;
input
<a href="http://abc.com">Title</a> <a href="http://xyz.com">Title</a>
becomes
<a href="http://abc.com">New Title</a> <a href="http://xyz.com">Title</a>
$string is your input, $result is your input modified. You can define more URLs as cases. Please note: I wrote that regular expression hastily, and I'm quite the novice. Please check that it suits all your intended cases.