Strip YouTube Embed Code Down to URL Only

后端 未结 3 422
一生所求
一生所求 2021-01-28 05:42

Please help!

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

$

        
                      
相关标签:
3条回答
  • 2021-01-28 06:19
    <?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]) );
    
    ?>
    
    0 讨论(0)
  • 2021-01-28 06:19

    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>');
    
    0 讨论(0)
  • 2021-01-28 06:19
    $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

    0 讨论(0)
提交回复
热议问题