0

会员现在可以输入 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);

?>

感谢安迪

4

1 回答 1

0

如果您想轻松更改分隔符,您可以将其放在一个常量字符串中,并将此常量插入到正则表达式模式中来代替分隔符本身。然后,当您想更改它时,只需编辑定义行:

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);

请注意,如果您选择在正则表达式中具有特殊含义的分隔符,则必须使用反斜杠对其进行转义。更多信息在这里

于 2013-06-03T06:58:30.297 回答