/中提取特定的 url 编码查询字符串片段标签

发布于 2024-11-16 16:08:43 字数 829 浏览 4 评论 0原文

我想删除所有内容,并且只想要 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 这个值,并想删除其他所有内容。

I want to remove everything, and only want a particular value between tags in php,
here is the code what I want:

<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>

I just only want 17181143 this value and want to remove everything else..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒数 2024-11-23 16:08:43

使用简单 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)

Using the Simple HTML DOM Library:

<?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;
                        }
                    }
                }
            }
        }
    }
?>

Output:

CODE: 17181143

Notes:

  • Requires external library (DOMDocument doesn't like that fragment)
  • Works with multiple embeds (should, anyways.. only tested with sample embed)
  • Uses PHP's URL parser (rather than RegEx)
你曾走过我的故事 2024-11-23 16:08:43

您可以使用 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);

You can use PHP and REGEX to pull out the numbers into an array and thereby discarding everything else.

$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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文