user1756209 user1756209 - 1 month ago 21
Java Question

Regex YouTube IFRAME


Possible Duplicate:

How to parse and process HTML with PHP?




I want to publish an app for android (wordpress blog). The app displays also some YouTube videos.
I need a method to replace YouTube IFRAMES + EMBED-Codes (old embed method) with an a - tag:

<iframe ...


<a href="http://www.youtube.com/watch?v=id"><img src="http://i1.ytimg.com/vi/id/hqdefault.jpg" /></a>


The replacement: either via php (server) or via java (eclipse). I have only found some snippets...

/embed\/([a-zA-Z0-9_-]{11})/


But how could I use it? Please help me!

EDIT: I try to replace the following CODE:

<iframe width="560" height="315" src="http://www.youtube.com/embed/**ID**" frameborder="0" allowfullscreen></iframe>


@Stephan

$content = "contains <iframe... and article"
$search = '/<iframe.+?src="http://www.youtube.com/embed/([a-zA-Z0-9_-]{11})"[^>]+?></iframe>/i';
$replace = '<a href="http://www.youtube.com/watch?v=$1"><img src="http://i1.ytimg.com/vi/$1/hqdefault.jpg" /></a>';
$content = preg_replace($search, $replace, $content);
echo $content;


How could I used it? Error: Unknown modifier '/' (in PHP).

Answer

Try this :

~<iframe.+?src="https?://www.youtube.com/embed/([a-zA-Z0-9_-]{11})"[^>]+?></iframe>~i


DEMO :

https://regex101.com/r/2mjn1r/1

Comments