Giddy Rob Posted January 12, 2008 Share Posted January 12, 2008 Hi, I was just wondering how to pick out some specific info from a variable. I am building a CMS system that allows the user to embed a youtube video on the site. I have specified on the page the size of the video player so I don't want to use all of the code provided by youtube. To make it as simple as possible for the user they just need to grab the code and copy and paste it into the relevant field for the database. e.g. <object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/SpJUj626qCc&rel=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/SpJUj626qCc&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object> The only bit of code I want is the address i.e. How can I get that info and put it into another variable, which just has the address? Cheers in advance guys Rob Quote Link to comment https://forums.phpfreaks.com/topic/85695-select-part-of-variable-contents/ Share on other sites More sharing options...
sKunKbad Posted January 12, 2008 Share Posted January 12, 2008 <?php $flashMovie = '<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/SpJUj626qCc&rel=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/SpJUj626qCc&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>'; $subject = "$flashMovie"; $pattern = '/http:\/\/www\.youtube\.com.*\"/'; preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3); print_r($matches[0][0]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/85695-select-part-of-variable-contents/#findComment-437363 Share on other sites More sharing options...
Giddy Rob Posted January 12, 2008 Author Share Posted January 12, 2008 Hi, thanks for your help. Just a couple of things. It seems to output "> at the end, which I don't want. How do I stop it just before the "> ? Also, it outputs both the pattern result and the subject data. I just want the pattern result of the preg_match. How do I achieve this? Cheers Rob Quote Link to comment https://forums.phpfreaks.com/topic/85695-select-part-of-variable-contents/#findComment-437389 Share on other sites More sharing options...
Giddy Rob Posted January 12, 2008 Author Share Posted January 12, 2008 hmmm, just realised what it's doing so please ignore earlier post. It is '/http:\/\/www\.youtube\.com.*\"/' that is wrong. it leaves this as a result: name="wmode" value="transparent"></param><embed src=" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355 looks like it's looking for the last " how do i just get it to stop at the next " in the variable? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/85695-select-part-of-variable-contents/#findComment-437415 Share on other sites More sharing options...
sKunKbad Posted January 13, 2008 Share Posted January 13, 2008 Sorry, I had to go leave, but now that I am back, I had a little more time. As long as the number of characters after "http://www.youtube.com" is 20, you could use this: <?php $flashMovie = '<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/SpJUj626qCc&rel=1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/SpJUj626qCc&rel=1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>'; $subject = "$flashMovie"; $pattern = '/http:\/\/www\.youtube\.com.{20}/'; preg_match($pattern, $subject, $theMatch); echo $theMatch[0]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/85695-select-part-of-variable-contents/#findComment-437766 Share on other sites More sharing options...
Giddy Rob Posted January 13, 2008 Author Share Posted January 13, 2008 Great stuff, it appears there are 20 characters after the videos (at least the ones I tested). Again, thanks for your help. I guess I should really learn that preg_match, but you have saved me some time so I salute you for that. Cheers Rob Quote Link to comment https://forums.phpfreaks.com/topic/85695-select-part-of-variable-contents/#findComment-437842 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.