0

私はこの機能を持っています:

function get_vk($url) {
    $str = file_get_contents("http://vk.com/share.php?act=count&index=1&url=" . $url);
    if (!$str) return 0;
    return preg_match('/^VK.Share.count\((\d+),\s+(\d+)\);$/i', $rq, $i) ? (int) $i[2] : 0;
}

ただし、この関数$strは NULL であるため、常に 0 を返します。しかし、このリンクを張るだけなら

https://vk.com/share.php?act=count&index=1&url=http://stackoverflow.com

ブラウザに戻りVK.Share.count(1, 43);ますどこに問題がありますか?

4

1 に答える 1

1

入力文字列を preg_match に渡していません。

コードは次のように記述します。

function get_vk($url) {
    $str = file_get_contents("http://vk.com/share.php?act=count&index=1&url=" . $url);
    if (!$str) return 0;
    preg_match('/^VK.Share.count\((\d+),\s+(\d+)\);$/i', $str, $matches);
    $rq = $matches[1];

    return $rq;
}

echo get_vk('http://stackoverflow.com');

http://php.net/preg_matchで preg_match の構文を読むことができます。

于 2013-02-24T21:52:32.603 に答える