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


How does recursion work in this case?

var arr = [7,3,28,8,9,13,1500,45];

function qsort(a) {
    if (a.length == 0) return [];

    var left = [], right = [], pivot = a[0];

    for (var i = 1; i < a.length; i++) {
        a[i] < pivot ? left.push(a[i]) : right.push(a[i]);
    }

    return qsort(left).concat(pivot, qsort(right));
}

alert(qsort(arr));

This routine sorts array by using Quicksort algorithm. The Question is how will the base case if (a.length == 0) return []; ever be true to stop the recursion?

4

2 に答える 2

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 に答える
0

シンプルな 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 ではなく)
于 2011-06-21T22:56:04.267 に答える