会员现在可以输入Google|http://www.google.com(输入)Bing|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);
?>
谢谢安迪
如果您想方便地更改分隔符,您可以将其放在一个常量字符串中,并将该常量插入到正则表达式模式中,以取代分隔符本身。然后当你想改变它时,只需编辑define行:
define ('DELIM', ':::');
$test_data = array(
'name1' . DELIM . 'http://www.example1.com', 'name2' . DELIM .'http://www.example2.com',
'http://www.example3.com?lang=en&title=title3', 'http://www.example4.com');
/**
* Extract url and name from input string and return them in an object.
*
* @param string $input
* @return object
*/
function split_link2($input) {
$first_char = substr(DELIM, 0, 1);
$subpattern = '[^' . $first_char . ''n]++';
if (strlen(DELIM)>1) {
$rest = substr(DELIM, 1);
$subpattern = '(?>' . $subpattern . '|' . $first_char . '(?!' . $rest .'))+';
}
$pattern = '~^(?J)(?>(?<name>' . $subpattern . ')' . DELIM
. ')?(?<url>https?+:'/'/.+?)(?>&title=(?<name>[^'n]++))?$~';
if (preg_match($pattern, $input, $match)) {
if ($match['name'] == '') $match['name'] = $match['url'];
return (object)array('url'=>$match['url'], 'name'=>$match['name']);
}
}
/**
* Display links from an array into an unordered list
*
* @param array $links
* @param integer $limit number of links displayed
* @param integer $groupby number of items per group
* @return void
*/
function print_links2($links, $limit, $groupby) {
echo '<ul class="flo-l-r">';
$nb_display = min(count($links), $limit);
for($i=0; $i<$nb_display; $i++) {
if (!($i % $groupby) && $i) echo "'n</ul>'n" . '<ul class="flo-l-r">';
$link = split_link2($links[$i]);
echo "'n't<li>"
. '<a rel="nofollow" target="_blank" href="'
. $link->url . '">' . $link->name . '</a></li>';
}
echo "'n</ul>'n";
}
print_links2($test_data, 40, 2);
注意,如果选择在正则表达式中具有特殊含义的字符作为分隔符,则必须用反斜杠转义它们。更多信息请点击