scvinodkumar Posted May 29, 2009 Share Posted May 29, 2009 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 Quote Link to comment Share on other sites More sharing options...
glenelkins Posted May 29, 2009 Share Posted May 29, 2009 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 ) ); ?> Quote Link to comment Share on other sites More sharing options...
glenelkins Posted May 29, 2009 Share Posted May 29, 2009 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 Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted May 29, 2009 Share Posted May 29, 2009 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; Quote Link to comment Share on other sites More sharing options...
glenelkins Posted May 29, 2009 Share Posted May 29, 2009 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? Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted May 29, 2009 Share Posted May 29, 2009 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. Quote Link to comment Share on other sites More sharing options...
glenelkins Posted May 30, 2009 Share Posted May 30, 2009 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" Quote Link to comment Share on other sites More sharing options...
glenelkins Posted May 30, 2009 Share Posted May 30, 2009 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? Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted May 30, 2009 Share Posted May 30, 2009 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. Quote Link to comment Share on other sites More sharing options...
scvinodkumar Posted June 1, 2009 Author Share Posted June 1, 2009 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 Quote Link to comment Share on other sites More sharing options...
scvinodkumar Posted June 1, 2009 Author Share Posted June 1, 2009 or please tell me how to replace the youtube url into embed code within the content. Waiting for your reply. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted June 1, 2009 Share Posted June 1, 2009 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.