2010-11-26 3 views
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; 

Retourné: http://site.com/http://site.com/http://site.com/video/TI - No+Matter+What/_CHheEQe9M8/liens Extrait de HTML

Pourquoi? Je veux: http://site.com/video/TI - No+Matter+What/_CHheEQe9M8/ Comment le faire? désolé pour le mauvais anglais

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; 

Ce code renvoie http://site.com/http://site.com/http://site.com/video/TI - No+Matter+What/_CHheEQe9M8/ aussi ...

+2

duplication possible de [Expression régulière pour saisir l'attribut href d'un élément A] (http://stackoverflow.com/questions/3820666/regular-expression-for-grabbing-the-href-attribute-of-an- un-élément) – Gordon

+1

@Gordon, je ne devrais pas saisir, mais changer – Isis

+0

@Isis n'a pas d'importance. même approche. ne pas me faire aller chercher le doublon pour cela :) – Gordon

Répondre

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); 

Résultat:

<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> 
+0

ouais .. merci: D – Isis

0

Change ^>^pour » commencer par qu'il prend à beaucoup comme lorsque je teste . il

Questions connexes