views:

261

answers:

2

Hi guys.

I have :

<a href="http://abc.com"&gt;Title&lt;/a&gt;

And :

<a href="http://xyz.com"&gt;Title&lt;/a&gt;

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.

+2  A: 

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"&gt;Title&lt;/a&gt;
<p>some more text</p>
<a href="http://xyz.com"&gt;Title&lt;/a&gt;
<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"&gt;
<html><body>
<p>some text</p>
Title
<p>some more text</p>
<a href="http://xyz.com"&gt;Title&lt;/a&gt;
<p>and some again</p>
</body></html>


Note that the whole <a href="http://abc.com"&gt;Title&lt;/a&gt; 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, ...

Pascal MARTIN
Thanks you, it works. But when I'm parsing content via RSS feed using Magpie RSS, the link I want to replace often dynamic. I mean in the <description> tag of RSS has links like http://abc.com/tag/tag1, http://abc.com/tag/tag2.... Can I replace all these links with a text?
nuphero
A: 

To cover another interpreted case:

$string = '<a href="http://abc.com"&gt;Title&lt;/a&gt; <a href="http://xyz.com"&gt;Title&lt;/a&gt;';
$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"&gt;Title&lt;/a&gt; <a href="http://xyz.com"&gt;Title&lt;/a&gt;

becomes

<a href="http://abc.com"&gt;New Title</a> <a href="http://xyz.com"&gt;Title&lt;/a&gt;

$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.

calurion