2011-01-17 9 views
0

Ich habe einige Probleme mit Preg_replace und preg_match_all, um eine Youtube-URL zum Einbetten von Code zu konvertieren. Ja, ich weiß, dass dieses Thema schon im Stackoverflow angefasst hat aber nicht genau wie ich es möchte.Hyperlink Youtube zum Einbetten Code

ich die ID von einer URL bekommen, ohne html, mit, dass:

http://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)(\w*)(&(amp;)?[\w\?=]*)? 

Aber ich habe das mit wie diese formatiert url:

<a href="http://www.youtube.com/watch?v=C9KAqhbIZ7o" class="comment-link">http://www.youtube.com/watch?v=C9KAqhbIZ7o</a> 

Und ich will es konvertieren das alles:

<object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&amp;hl=es_ES&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&amp;hl=es_ES&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object> 

Jemand kann etwas Magie tun und sagen Sie mir, den richtigen Ausdruck alle die uRL zu erkennen, erhalten die ID auf ce und konvertieren alle in einen Einbettungscode? Vielen Dank im Voraus!

Aktualisierung der Informationen:

Um zu helfen und macht es zu einem prägnanten ...

Ich habe dies:

<p>This is an example of comment</p><strong>Hi bold!</strong><p>Look a youtube url! <a href="http://www.youtube.com/watch?v=C9KAqhbIZ7o" class="comment-link">http://www.youtube.com/watch?v=C9KAqhbIZ7o</a></p> 

Und ich will diese bekommen:

<p>This is an example of comment</p><strong>Hi bold!</strong><p>Look a youtube url! <object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&amp;hl=es_ES&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/C9KAqhbIZ7o?fs=1&amp;hl=es_ES&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object></p> 

Vielen Dank für Ihre Hilfe, ich schätze es wirklich!

+0

Wie werden Sie die "unformatiert url" bekommen? weil der href-Parameter des a-Elements schneller ist. –

+0

Danke für Ihren Kommentar. Die URL wird automatisch formatiert, weil Wordpress sie konvertiert und ich möchte, dass sie so läuft. Und ja, ich weiß, ist schneller, aber ich möchte alle a-Element ersetzen, nicht nur die ID des Videos. – miduga

Antwort

2

Ersetzen Sie $ html durch Ihre HTML-Zeichenfolge, die analysiert werden muss.

$html=<<<HTML 
<p>This is an example of comment</p><strong>Hi bold!</strong><p>Look a youtube url! <a href="http://www.youtube.com/watch?v=C9KAqhbIZ7o" class="comment-link">http://www.youtube.com/watch?v=C9KAqhbIZ7o</a></p> 

HTML; 


$regex="/v\=([\-\w]+)/"; 

preg_match_all($regex,$html,$out); 

$out[1]=array_unique($out[1]); 

foreach($out[1] as $o){ 

     $reg="/(<a).*(youtube.com).*($o).*(\/a>)/"; 

     $embed= <<<HTML 
     <object width="640" height="505"><param name="movie" value="http://www.youtube.com/v/$o=1&amp;hl=es_ES&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/$o?fs=1&amp;hl=es_ES&amp;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="505"></embed></object> 
HTML; 

     $html=preg_replace($reg,$embed, $html); 

} 

echo $html; 
+0

Vielen Dank !! :) Es funktioniert nicht wie erwartet, weil ich den gesamten Kommentartext erhalten und nur die URL des Videos durch den Einbettungscode ersetzen und die URL einmal ersetzen muss (ich habe es getestet und zweimal konvertiert). Danke für die Bemühung!!! – miduga

+0

Es funktioniert jetzt ... doppelte Einträge aus dem Array entfernt. –

+0

Es funktioniert perfekt !!!!! :) Danke Danke Danke !! Du weißt nicht, wie viele Stunden ich damit verbracht habe. Vielen Dank!!! – miduga

3

Ich benutze diesen Code

 // url of video 
    $url = $row['url']; 
    $id=0; 
    // we get the unique video id from the url by matching the pattern 
    preg_match("/v=([^&]+)/i", $url, $matches); 
    if(isset($matches[1])) $id = $matches[1]; 
    if(!$id) { 
     $matches = explode('/', $url); 
     $id = $matches[count($matches)-1]; 
    } 
    // this is your template for generating embed codes 
    $code = '<div id="img_wrapper"><object width="640" height="458"><param name="movie" value="http://www.youtube.com/v/{id}&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/{id}&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></div>'; 

    // we replace each {id} with the actual ID of the video to get embed code for this particular video 
    $code = str_replace('{id}', $id, $code); 

    echo $code; 
+0

Nun, es kann funktionieren, also muss ich jetzt nur noch ein preg_match bekommen, um alle URLs von youtube zu bekommen, das ist mein großes Problem !! Ich habe die Anfrage aktualisiert, um expliziter zu sein! Danke! – miduga

Verwandte Themen