开发者

Function preg_match() is not returning any value

I'm using preg_match() to match a pattern from a string and parse them further... But it's not returning any value...

Actually, I'm using that to fetch the videos by looking after the header information.

$url = "http://www.youtube.com/watch?v="开发者_StackOverflow中文版 . $video_id;
$this->req =& new HTTP_Request($url);
$response = $this->req->sendRequest();
$rescode  = $this->req->getResponseCode();

echo "++response code++$rescode";
echo "***$response***";

if (PEAR::isError($response)) {
    echo "<b>Please check whether the video added or not </b>";
} 
else {
    $page = $this->req->getResponseBody();
    //echo "=====$page====";

    preg_match('/"videoplayback": "(.*?)"/', $page, $match);

    $var_id = $match[1];
    echo "+++$var_id+++;
}

All are working fine if I replace the preg_match call with the below code, but that’s not what I need..

preg_match('/"video_id": "(.*?)"/', $page, $match);

The string which I'm trying to match is:

http://v17.lscache6.c.youtube.com/videoplayback?ip=0.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor%2Coc%3AU0dYR1RMVl9FSkNNOF9MRlpB&fexp=906320&algorithm=throttle-factor&itag=34&ipbits=0&burst=40&sver=3&expire=1292418000&key=yt1&signature=32DD3C5BF016489B43D15493F68ACCDE2AA720B5.95F66ACBE6FDFBC60978A14075A28020E083E8A8&factor=1.25&id=39743a9fde40b0dd&

How can I fix this?


If all you need is to check for the presence of the "videoplayback" in $page, then you could just use:

if (preg_match("/.*videoplayback.*/", $page){
    // Use the whole $page URL
}

There is isn't any need to trap any internal match components.

You may want to use a tighter pattern, though:

$pattern = "/.*youtube\.com\/videoplayback\?.*/";


Try:

preg_match('/\"videoplayback\"\:\s\"(.*)\"/iU', $page, $match);

If it doesn’t work, also try this:

preg_match('/\"videoplayback\"\:\s\"([^\"]*)\"/iU', $page, $match);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜