如何解决在<object> / <embed>标记中提取特定的URL编码查询字符串
|| 我想删除所有内容,并且只希望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这个值,并想要删除其他所有内容。
解决方法
使用简单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)
, 您可以使用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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。