0

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


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);
4

2 回答 2

0

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)
于 2011-06-21T22:56:04.267 回答
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);
于 2011-06-21T21:59:28.423 回答