0

I have the following data in mysql database which is an iframe from u-tube:-

`<iframe width="560" height="315" src="http://www.youtube.com/embed/Om2fabTIKE4" frameborder="0" allowfullscreen></iframe>`

Nevertheless, in the php codes, a present phase is present for the above iframe, in which I just need Om2fabTIKE4 as the variable to be filled in.

I would like to ask, is there any way that I can trim away

<iframe width="560" height="315" src="http://www.youtube.com/embed/

and

frameborder="0" allowfullscreen></iframe>

1
  • sounds like a job for preg_replace(), preg_match()
    – user557846
    Apr 30, 2012 at 5:59

2 Answers 2

2

Try this:

$html = '<iframe width="560" height="315" src="http://www.youtube.com/embed/Om2fabTIKE4" frameborder="0" allowfullscreen></iframe>';
$dom = new DOMDocument();                                                                           
$dom->loadHTML($html);                                                                              
$tags = $dom->getElementsByTagName('iframe');                                                       
foreach ($tags as $tag)                                                                             
  $link = explode('/',parse_url($tag->getAttribute('src'),PHP_URL_PATH));                           
var_dump($link[2]);      

You can learn more about the DOMDocument class here.

0

Use str_replace to remove the text

2
  • it works! However, I realize, when the u-tube iframe is width="420" height="315", the str_replace can no longer works. Would there be another expression that can recognize the length of the string, or recognize "<iframe" to "embed/"??
    – Ham
    Apr 30, 2012 at 6:25
  • a simple method is to run it twice once for 560 and another for 420 check out regular expression if you want to do it in a single line. Use strlen to get the length of a string. you can also explode the string with a delimiter of embed/
    – anselm
    Apr 30, 2012 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.