Hyperlink Youtube to Embed Code
Asked Answered
G

2

0

I've some troubles using Preg_replace and preg_match_all to convert a Youtube URL to embed code. Yes, I know that this topic has already touched in stackoverflow but not exactly like I want.

I can get the ID from a url, without html, with that:

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

But I've the url formatted with like this:

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

And I want to convert it all to this:

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

Somebody can do some magic and tell me the correct expression to detect all the url, get the ID once and convert all to an embed code? Thank you so much in advance!

Update information:

In order to help and make it a more concise...

I've this:

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

And I want to get this:

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

Thanks all for your help, I really appreciate it!

Gallaway answered 17/1, 2011 at 12:48 Comment(2)
How are you getting the "unformatted url"? because getting the href parameter of the a element is quicker.Seismograph
Thanks for your comment. The URL es formatted automatically, because Wordpress converts it and I want to keep it running this way. And yeah, I know is quicker but I want to replace all the a element, not only get the ID of the video.Gallaway
S
2

Replace $html with your html string which needs parsing.

$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;
Seismograph answered 20/1, 2011 at 9:51 Comment(2)
Many thanks!! :) It doesn't work as expected, because I need to conserve all the comment text and only replace the url of the video with the embed code and replace the url once (I've tested it and converts twice). Thanks for the effort!!!Gallaway
It works perfect!!!!! :) Thanks thanks thanks thanks!! You don't know how many hours I've spent on this. Thanks!!!Gallaway
I
3

I use this 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;
Internee answered 20/1, 2011 at 11:0 Comment(1)
Well it can work so I only have now to get a preg_match yo get all the urls of youtube, that is my big problem!! I've updated the request in order to be more explicit! Thankssss!!Gallaway
S
2

Replace $html with your html string which needs parsing.

$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;
Seismograph answered 20/1, 2011 at 9:51 Comment(2)
Many thanks!! :) It doesn't work as expected, because I need to conserve all the comment text and only replace the url of the video with the embed code and replace the url once (I've tested it and converts twice). Thanks for the effort!!!Gallaway
It works perfect!!!!! :) Thanks thanks thanks thanks!! You don't know how many hours I've spent on this. Thanks!!!Gallaway

© 2022 - 2024 — McMap. All rights reserved.