Jump to content

Recommended Posts

HI,

 

How to convert the youtube url into embed code

 

Example,

suppose, if i post the url "

in the comment box, then it you should convert into embeded code, like this

 

"<object width="425" height="344"><param name="movie" value="

name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="
type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>"

 

when showing the comment in the display area.

 

I hope you peoples understand my english.

 

Thanks for your help :)

 

 

Link to comment
https://forums.phpfreaks.com/topic/160146-convert-youtube-url-into-embed-code/
Share on other sites

This would do it from the example embed code you gave:

 

<?php

$new_url = 'http://www.test.com';

$subject = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/wTnChGG2CM0&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/wTnChGG2CM0&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>';

$pattern = '%([.+?]*)value=".+?"([.+?]*)%';// Replaces value="URL"
$pattern2 = '%([.+?]*)src=".+?"([.+?]*)%'; // Replaces src="URL"

$subject = preg_replace ( $pattern, '$1' . 'value="' . $new_url . '"' . '$2', $subject, 1 );
$subject = preg_replace ( $pattern2, '$1' . 'src="' . $new_url . '"' . '$2', $subject, 1 );

echo ( htmlentities ( $subject ) );
?>

though i do have a question to ask whoever is reading. I just wrote those patterns, but what I don't understand is why the [.+?]  will not work with the [] around it in the parts value="" and src=""  and if i take out the [] where they are now it prevents it working as well

though i do have a question to ask whoever is reading. I just wrote those patterns, but what I don't understand is why the [.+?]  will not work with the [] around it in the parts value="" and src=""  and if i take out the [] where they are now it prevents it working as well

 

This is a character class that is saying, at the current location in the string, check to see if that character is a literal dot , plus or question mark character. What is important to understand is that inside a character class, almost all meta characters lose their special meaning (including those characters you listed).

 

And if I understand the OP's request correctly...

 

$url = 'http://www.youtube.com/watch?v=wTnChGG2CM0';
$pattern = '#http://www\.youtube\.com/watch\?([a-z])=([a-z])([a-zA-Z0-9]+)#';
$replace = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/$1/$2$3&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/$1/$2$3&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>';
$url = preg_replace($pattern, $replace, $url);
echo $url;

right this is where i get confused.

 

the other day someone told me to replace .*  with .+? to check for any character 0 or more times. Now your saying that inside the [] this actually is only checking for . + and ? but outside the [] it says any char, one or more times ... and the Q mark means?

I think you had that conversation with me on that one.

The question mark (outside the character class [...]) makes a quantifier (quantifiers are characters like * (zero or more times) or + (one or more times) lazy). Otherwise, it is considered to make the previous character (or group of characters) optional. I do believe I posted this link before, but I'll again point to it instead of repeating everything I explained (along with CV's demonstration) all over again. Have a look at this thread (please read post #11 and #14 as they explain the effects of making quantifiers lazy vs being greedy).

 

So yeah, inside a character class [...], most special (meta) characters (like the dot, + and ? for example) lose their special meaning and become literals. Otherwise, those meta characters retain their special abilities (unless you manually escape them using the backslash). So in a pattern, if I do this:

 

'#www.somesite.com#'

 

those dots are wildcards that accept any single character (other than a newline - \n) by default.. so while those dots can match a dot, that pattern can match a string like - www!somesite*com, as the dots are wildcards..

 

if however, I escape them like so:

 

'#www\.somesite\.com#'

 

this now forces the regex engine to look for www(dot)somesite(dot)com. These dots in the pattern are no longer dot_match_all wildcards, as they are escaped (and are thus treated as literals). Same thing goes for any meta character.. if you want to look for a literal ?, you must escape it within the pattern: \? (if it is not listed within a character class that is), otherwise, the engine sees this as either (optional), or if it comes after a quantifier (like * or +), it's treated as making those quantifiers lazy.

 

Hope that helps clear things somewhat.

ok i see!

 

so really in this example the 2 patterns i made would work like this:

 

$pattern = '%(.*)value=".+?"(.*)%';// Replaces value="URL"
$pattern2 = '%(.*)src=".+?"(.*)%'; // Replaces src="URL"

so really using .+? is not the best way, maybe something along the lines of

 

[a-z0-9/:._-?&]*  would work better in this scenario?

 

or perhaps just nothing at all so it will match everything?

 

ok i see!

 

so really in this example the 2 patterns i made would work like this:

 

$pattern = '%(.*)value=".+?"(.*)%';// Replaces value="URL"
$pattern2 = '%(.*)src=".+?"(.*)%'; // Replaces src="URL"

 

Not sure if it would work (as I didn't try them out.. if you did, you would know the results), but it is much more likely to succeed now that those characters are not within a character class. But again, in the link I provided, using .* is generally not a wise idea (as that thread explains the potential speed / accuracy issues). If you are going to use .*, depending on the circumstances, it might be wiser to use .*? instead...

 

so really using .+? is not the best way, maybe something along the lines of

 

[a-z0-9/:._-?&]*  would work better in this scenario?

 

or perhaps just nothing at all so it will match everything?

 

 

If you can use character classes instead, they are overall faster than .*? or .+? But sometimes you're better off using one method over the other (sometimes, you can't always use a character class). It truly is circumstantial.

 

Just to note in your character class: [a-z0-9/:._-?&]

When I mentioned that most meta characters lose their special meaning, there are a few that don't (depending on where they are located within the class). Some examples include ^ and -

 

In this case, notice the location of your dash... if the dash is not the very first (or very last) character listed in the class, it is treated as a range character (much like in say for example, [a-z]). So in your character class, you have in effect created a range from _ to ?. So you should do either of the following instead:

 

[a-z0-9/:._?&-]* or [-a-z0-9/:._?&]*

 

When the dash is the first or last character, the regex engine cannot treat it as a range, as there needs to be characters on both side of the dash in order for a range to occur.

 

Another meta character that might retain its ability within a character class is the ^ character. If it is the first one listed within the class, this makes the whole class negated...otherwise, if that character is located elsewhere within the class, it is treated as a literal.

Dear friends,

 

Thanks for all your help.

 

This works for me.

 

Also, I got another situation, when the youtube url posted with content. so, Could you please tell me how to separate the youtube url from the content.

 

Example,

$content    = "Over the years I have worked at many client sites and a variety of office layouts. On one project in particular, we had as many as 80 people in a project team, seated via an open plan arrangement. 
               <br> http://www.youtube.com/watch?v=ElUomP6Y_kg&feature=popular <br>
               Over the years I have worked at many client sites and a variety of office layouts. On one project in particular, we had as many as 80 people in a project team, seated via an open plan arrangement.";

 

Now i need to get the youtube url into variable and then only i can able to apply your ideas.

 

i hope you understand this.

 

Thanks for all your efforts

 

There is no need for regular expressions in this case:

 

<?php
$url = 'http://www.youtube.com/watch?v=wTnChGG2CM0';

parse_str(parse_url($url, PHP_URL_QUERY), $qstring);

echo <<<EOF
<object width="425" height="344">
<param name="movie" value="http://www.youtube.com/v/{$qstring['v']}&hl=en&fs=1"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/{$qstring['v']}&hl=en&fs=1"
       type="application/x-shockwave-flash"
       allowscriptaccess="always"
       allowfullscreen="true"
       width="425"
       height="344"></embed>
</object>
EOF;
?>

 

Edit: That is of course only in the case you have the URL handy. You'll still need regex for the problem in your most recent post. Just wanted to point out that PHP has built-in functions for parsing and generating URLs.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.