开发者

php preg_macthing for tumblr avatar grabbing

amm please kindly check this code? cos it won't work..

<?php
$page = file_get_contents("http://natadec0c0.tumblr.com/");
$avatar = preg_match('/<a rel="shortcut icon" href="(http+)"/', $page, $matches) ? $matches[1]: 'http://27.media.tumblr.com/avatar_749f6bc22627_128.png';

echo $avatar;
?>

To Grab copy this tag view-source:http://natadec0c0.tumblr.com/

<link rel="shortcut icon" href="http://25.media.tumblr.com/avatar_fa85d9b5e571_16.png" />

whew almost half开发者_如何学编程 of hour im stuck in this problem.. hope someone can help me...


You're searching for an <a rel..., instead of a <link rel....

Also, you'll likely want something like (http.+?) instead of (http+).

A better better way for finding that link though would be something like:

if (preg_match('/<link.+?>/si', $page, $link_matches)
    && strpos($link_matches[0], 'shortcut icon') !== false
    && preg_match('/href\s*=\s*"(http:.+?)"/si', $link_matches[0], $matches))
{
    $avatar = $matches[1];
}
else
{
    $avatar = 'http://27.media.tumblr.com/avatar_749f6bc22627_128.png';
}

In the above example, you'd first be looking for <link/> tags, then look for the href attribute in the found link tags. Just in case they look a little differently from what you expect them to look like.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜