Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/85695-select-part-of-variable-contents/
Share on other sites

<?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]);
?>

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

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

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];
?>

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

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.