在<object> / <embed />标记</object>中提取特定的url编码的查询字符串

时间:2011-06-21 21:54:32

标签: php tags soundcloud

我想删除所有内容,只想在php中标记之间的特定值, 这是我想要的代码:

<object height="81" width="100%">
    <param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F17181143"></param> <param name="allowscriptaccess" value="always"></param>
    <embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F17181143" type="application/x-shockwave-flash" width="100%"></embed>
</object>
<span>
    <a href="http://soundcloud.com/kiwinest/linkin-park-iridescent">Linkin Park - Iridescent</a> by <a href="http://soundcloud.com/kiwinest">KiwiNest</a>
</span>

我只想要 17181143 这个值,并希望删除其他所有内容..

2 个答案:

答案 0 :(得分:0)

您可以使用PHP和REGEX将数字拉出到数组中,从而丢弃其他所有内容。

$my_string = '<object height="81" width="100%"> <param name="movie" 
value="http://player.soundcloud.com/player.swf?url=http%3A%2F 
%2Fapi.soundcloud.com%2Ftracks%2F17181143"></param> 
<param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" 
height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F  
%2Fapi.soundcloud.com%2Ftracks%2F17181143" type="application/x-shockwave-flash" width="100%">
</embed> </object>  <span><a href="http://soundcloud.com/kiwinest/linkin-park-iridescent">Linkin 
Park - Iridescent</a> by <a href="http://soundcloud.com/kiwinest">KiwiNest</a></span>';

// Assuming all the numbers are going to be 8 characters long, if they are not, then just change
// the regex.
// look for all numbers that are 8 characters long.
preg_match_all('/[0-9]{8}/', $my_string, $ids);

答案 1 :(得分:0)

使用Simple HTML DOM资料库:

<?php
    include('lib/simple_html_dom.php');

    $string = '<object height="81" width="100%"> <param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F17181143"></param> <param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F17181143" type="application/x-shockwave-flash" width="100%"></embed> </object>  <span><a href="http://soundcloud.com/kiwinest/linkin-park-iridescent">Linkin Park - Iridescent</a> by <a href="http://soundcloud.com/kiwinest">KiwiNest</a></span>';

    $html = str_get_html($string);
    //$html = file_get_html('http://www.mysite.com/');

    if ($html) {
        foreach ($html->find('object') as $obj) {
            foreach ($obj->find('param') as $par) {
                if ($par->name == 'movie') {
                    $embed = parse_url($par->value);
                    parse_str(urldecode($embed['query']), $val);
                    if (array_key_exists('url', $val)) {
                        $url = parse_url($val['url']);
                        $path = explode('/', $url['path']);
                        $code = array_pop($path);
                        if (is_numeric($code)) {
                            echo 'CODE: ' . $code . PHP_EOL;
                        }
                    }
                }
            }
        }
    }
?>

<强>输出:

CODE: 17181143

备注:

  • 需要外部库(DOMDocument不喜欢该片段)
  • 使用多个嵌入(无论如何都应该使用样本嵌入进行测试)
  • 使用PHP的URL解析器(而不是RegEx)