0

A member is now able to input Google|http://www.google.com (enter) Bing|http://www.bing.com (enter etc) to get nice looking links on his personal page. The output is li-ists with links: Google Bing... Now the problem is that not everyone knows how to press "|". Therefore wonder if anyone have some idea how to use for ex. ":::" or something simple. Or just retrive link name from http... The code looks like this:

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

?>

Thanx Andy

  • This can be accomplished in pure CSS using [this](http://stackoverflow.com/questions/9171699/add-a-pipe-separator-after-items-in-an-unordered-list-unless-that-item-is-the-la) technique – Orangepill Jun 03 '13 at 02:30
  • No, unfortunately, this is a PHP question. I've already design the output so it looks something like this etc with normal borders on the left side of each
  • . The problem is when a member enter his links in the textarea. The don't want to use the "|" (pipesign) like this: Text 1|http://somelink1.com etc
  • – limetree Jun 03 '13 at 03:16