TinyURL 链接转换脚本

Jul 4th, 2007 | Filed under PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
 * transform
 *
 * @param mixed $url
 * @access public
 * @return void
 */
function transform($url) {
    if (empty($url) or !preg_match('|^(?:http://)?([^/]+)|i', $url)) {
        return;
    }
 
    $url     = "http://tinyurl.com/create.php?url=$url";
    $content = file_get_contents($url);
    $pattern = '|<blockquote><b>(http://tinyurl\.com/([^<]+))</b><br><small>|i';
    preg_match($pattern, $content, $matches);
    return $matches[1];
}
 
// }}}
 
 
// {{{ revert($tinyurl)
 
/**
 * revert
 *
 * @param mixed $tinyurl
 * @access public
 * @return void
 */
function revert($tinyurl) {
    $url     = explode('.com/', $tinyurl);
    $url     = 'http://preview.tinyurl.com/' . $url[1];
    $preview = file_get_contents($url);
    $pattern = '/redirecturl" href="(.*)">/i';
    preg_match($pattern, $preview, $matches);
    return $matches[1];
}
 
// }}}
 
/* vim: set expandtab tabstop=4 shiftwidth=4: */

无法使用中文URL,官方转换的 URL 如果其中还有中文似乎也有问题。 😀

Tags: ,
Comments are closed.