1
    <?php
    $cont = '<div class="video-image">
        <a href="video/TI - No+Matter+What/_CHheEQe9M8/" title="23">
            <img src="http://i.ytimg.com/vi/_CHheEQe9M8/3.jpg" alt="TI" width="130" height="78"/>
        </a>
        <span class="video-title"><a href="video/TI - No+Matter+What/_CHheEQe9M8/" title="sdg">No Matter What</a></span>
        <span class="video-artist"><a href="video/TI - No+Matter+What/_CHheEQe9M8/" title="ss" class="ellipsis">TI</a></span>
    </div>';

    if (preg_match_all('#<a href="([^>]*)"#iU', $cont, $arr))
    {
        foreach ($arr[1] as $value)
        {
            var_dump($value);
            $cont = preg_replace('#' . preg_quote($value, '#') . '#iU', 'http://site.com/' . $value, $cont);
        }
    }

    echo $cont;

Returned: http://site.com/http://site.com/http://site.com/video/TI - No+Matter+What/_CHheEQe9M8/

Why? I want : http://site.com/video/TI - No+Matter+What/_CHheEQe9M8/ How to do it? sorry for bad english

EDIT

$dom = new DOMDocument;
    $dom->loadHTML($cont);
    foreach( $dom->getElementsByTagName('a') as $node )
    {
        $cont = preg_replace('#' . preg_quote($node->getAttribute('href'), '#') . '#', "http://site.com/" . $node->getAttribute('href'), $cont);
    }    
    echo $cont;

This code returns http://site.com/http://site.com/http://site.com/video/TI - No+Matter+What/_CHheEQe9M8/ too...

mario
  • 144,265
  • 20
  • 237
  • 291
Isis
  • 4,608
  • 12
  • 39
  • 61
  • 2
    possible duplicate of [Regular expression for grabbing the href attribute of an A element](http://stackoverflow.com/questions/3820666/regular-expression-for-grabbing-the-href-attribute-of-an-a-element) – Gordon Nov 26 '10 at 15:46
  • 1
    @Gordon, I should not grab, but change – Isis Nov 26 '10 at 15:54
  • @Isis doesnt matter. same approach. dont make me fetch the duplicate for that :) – Gordon Nov 26 '10 at 15:56
  • @Gordon, I have a problem with replacing links with DOMDocument... – Isis Nov 26 '10 at 16:08
  • @Isis [search if it has been answered before](http://stackoverflow.com/search?q=replace+href+dom+php) and if not make it into a question – Gordon Nov 26 '10 at 16:11
  • *(hint)* you only want to prepend the href attribute with your string. – Gordon Nov 26 '10 at 16:17
  • @Gordon, I did it. $node->setAttribute('href', "http://site.com/" . $node->getAttribute('href')); How to echo this? $dom->saveHTML(); is returned; with doctype/html/body – Isis Nov 26 '10 at 16:22
  • @Isis you seem to have a valid XHTML fragment there so try `$dom->loadXml($cont)` and `echo $dom->saveXml($dom->documentElement)` instead. If you just want to echo the href attributes, just do `echo $node->getAttribute('href')` after you changed them. – Gordon Nov 26 '10 at 16:27
  • I just do echo all $cont.. sorry for bad english... echo $dom->saveXml($dom->documentElement); returned my $cont with – Isis Nov 26 '10 at 16:37

2 Answers2

2
$dom = new DOMDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXml($xhtml);
foreach( $dom->getElementsByTagName('a') as $node )
{
    $node->setAttribute(
        'href', 
        "http://site.com/" . $node->getAttribute('href')
    );
}
$dom->formatOutput = TRUE;
echo $dom->saveXML($dom->documentElement);

Result:

<div class="video-image">
  <a href="http://site.com/video/TI - No+Matter+What/_CHheEQe9M8/" title="23">
    <img src="http://i.ytimg.com/vi/_CHheEQe9M8/3.jpg" alt="TI" width="130" height="78"/>
  </a>
  <span class="video-title">
    <a href="http://site.com/video/TI - No+Matter+What/_CHheEQe9M8/" title="sdg">No Matter What</a>
  </span>
  <span class="video-artist">
    <a href="http://site.com/video/TI - No+Matter+What/_CHheEQe9M8/" title="ss" class="ellipsis">TI</a>
  </span>
</div>
Gordon
  • 312,688
  • 75
  • 539
  • 559
0

Change ^> to ^" to start with as it picks to much as it is when I test it.

David Mårtensson
  • 7,550
  • 4
  • 31
  • 47