0

Please help!

I need to strip the following code so that it only uses the "value" part

$<object width="360" height="226"><param name="movie" value="http://www.youtube.com/v/IkZuQ-aTIs0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/IkZuQ-aTIs0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="360" height="226"></embed></object>

So in this case it would strip it down to http://www.youtube.com/v/IkZuQ-aTIs0

The catch is that this is dynamic so it is pulling these embed codes for different files so they are changing.

Please help :D

cpcdev
  • 1,130
  • 3
  • 18
  • 45

3 Answers3

1
<?php

$string = '<object width="360" height="226"><param name="movie" value="http://www.youtube.com/v/IkZuQ-aTIs0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/IkZuQ-aTIs0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="360" height="226"></embed></object>';

preg_match_all('#http://www.youtube.com/v/([\w\-]+){11}#is', $string, $matches);

print_r( array_unique($matches[0]) );

?>
Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
  • @Brad, I know. I'm not parsing HTML, just extracting URL. DOMDocument is overkill in this case. – Dejan Marjanović Dec 16 '11 at 18:10
  • Perhaps, but if it were me, I'd opt towards the standard solution, unless you could guarantee the input format. DOMDocument won't be the fastest solution by any means, but if used correctly, you could traverse the document more reliably. – Brad Dec 16 '11 at 18:12
0

The best way is to use a DOM parser.

http://php.net/manual/en/class.domdocument.php

$doc = new DOMDocument();
$doc->loadHTML('<object width="360" height="226"><param name="movie" value="http://www.youtube.com/v/IkZuQ-aTIs0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/IkZuQ-aTIs0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="360" height="226"></embed></object>');
Brad
  • 159,648
  • 54
  • 349
  • 530
  • The VideoEmbed variable is what stores the YouTube Embed code that I'm trying to strip out. How can I add the code you all provided to this section if it is where I am supposed to add it? Sorry for the confusion I'm having a hard time displaying the code on here correctly I'm a noob View Video"; }?> – cpcdev Dec 16 '11 at 16:47
  • I made a new post that explains my problem better - http://stackoverflow.com/questions/8537550/rewrite-youtube-url – cpcdev Dec 16 '11 at 17:04
0
$string = '<object width="360" height="226"><param name="movie" value="http://www.youtube.com/v/IkZuQ-aTIs0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/IkZuQ-aTIs0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="360" height="226"></embed></object>';

$start = strpos($string, 'value="');
$string = substr($string, $start + 7);
$end = strpos($string, '" ');
$string = substr($string, 0, $end);

echo $string;

little more complex than webartos, but will grab any value, instead of just the youtube link

Ascherer
  • 8,223
  • 3
  • 42
  • 60
  • Thank you everyone for the fast feedback! I think I need to be more specific... basically the data for this is stored in XML format and I'm calling it like this [code] – cpcdev Dec 16 '11 at 16:38
  • Do not parse HTML in this manner. What happens when you get to some broken HTML? Let the standard classes built for this sort of thing do it, rather than re-invent the wheel. – Brad Dec 16 '11 at 18:08
  • And ive also had terrible luck with DOMDocument.... not really a big fan of that library – Ascherer Dec 16 '11 at 19:23