会员现在可以输入 Google| http://www.google.com(输入)必应| http://www.bing.com(输入等)在他的个人页面上获得漂亮的链接。输出是带有链接的列表:Google Bing... 现在的问题是不是每个人都知道如何按“|”。因此想知道是否有人知道如何使用 for ex。“:::”或简单的东西。或者只是从 http 中检索链接名称...代码如下所示:
<?php
// Split input string into name and url. If input is a plain link, then
// name == url. Returns link(name, url) object.
//
function split_link($input)
{
static $patterns = array
(
"@(.*?)\|(.*)@", // name|url
"@https?://.*\?.*title=(.*)(&.*)*@", // url&title=name
"@https?://.*?/(.*)@", // name from server path
"@(.*)@" // catch all
);
foreach($patterns as $key => $pattern) {
$match = array();
if(preg_match($pattern, $input, $match)) {
// print_r($match);/* uncomment for debug */
if($key == 0) {
$match['url'] = $match[2];
$match['name'] = $match[1];
} elseif($key == 3) {
$match['url'] = $match[1];
$match['name'] = $match[1];
} else {
$words = explode("|", strtr($match[1], "/-_", "|||"));
$match['url'] = $match[0];
$match['name'] = implode(" ", $words);
}
// printf("pattern %d matched %s\n", $key, $input);
// printf("name: '%s', url: '%s'\n", $match['name'], $match['url']);
break;
}
}
return (object)$match;
}
function print_links(&$arr, $max, $split)
{
printf("<ul class=\"flo-l-r\">\n");
foreach($arr as $index => $link) {
if($index >= $max) {
break;
}
if($index % $split == 0 && $index != 0) {
printf("</ul>\n");
printf("<ul class=\"flo-l-r\">\n");
}
$link = split_link($link);
printf(" <li><a rel='nofollow' target='_blank' href=\"%s\">%s</a></li>\n", $link->url, $link->name);
}
printf("</ul>\n");
}
$arr = explode("\r\n", (string)$data);
print_links($arr, 80, 4);
?>
感谢安迪