Advertisement
Guest User

cgmp_parse_wiki_style_links

a guest
Mar 28th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.01 KB | None | 0 0
  1. if ( !function_exists('cgmp_parse_wiki_style_links') ):
  2.     function cgmp_parse_wiki_style_links($text) {
  3.  
  4.         // Google Maps link : #*
  5.         $pattern = "/\#\*[^\#\*]*\#\*/";
  6.         preg_match_all($pattern, $text, $gmaplinks);
  7.  
  8.         if (isset($gmaplinks[0])) {
  9.             foreach ($gmaplinks[0] as $gmaplink)  {
  10.                 $text = str_replace($gmaplink, "[TOKEN]", $text);
  11.                 $gmaplink = preg_replace("/(\#\*)/", "", $gmaplink);
  12.                 $anchor = "<a target='_blank' href='http://maps.google.fr/maps/?q=".$gmaplink."'>Voir sur Google Maps</a>";
  13.                 $text = str_replace("[TOKEN]", $anchor, $text);
  14.             }
  15.         }
  16.  
  17.         // <br> : #-
  18.         $pattern = "/\#\-[^\#\-]*/";
  19.         preg_match_all($pattern, $text, $newlines);
  20.  
  21.         if (isset($newlines[0])) {
  22.             foreach ($newlines[0] as $newline)  {
  23.                 $text = str_replace($newline, "[TOKEN]", $text);
  24.                 $newline = preg_replace("/(\#\-)/", "", $newline);
  25.  
  26.                 $anchor = "<br>".$newline;
  27.                 $text = str_replace("[TOKEN]", $anchor, $text);
  28.             }
  29.         }
  30.  
  31.  
  32.         // Images : #@
  33.         $pattern = "/\#\@[^\#\@]*\#\@/";
  34.         preg_match_all($pattern, $text, $images);
  35.  
  36.         if (isset($images[0])) {
  37.             foreach ($images[0] as $image)  {
  38.                 $text = str_replace($image, "[TOKEN]", $text);
  39.                 $image = preg_replace("/(\#\@)/", "", $image);
  40.  
  41.                 $anchor = "<img src='".$image."'>";
  42.                 $text = str_replace("[TOKEN]", $anchor, $text);
  43.             }
  44.         }
  45.  
  46.         // Links : #
  47.         $pattern = "/\#[^\#]*\#/";
  48.         preg_match_all($pattern, $text, $wikilinks);
  49.  
  50.         if (isset($wikilinks[0])) {
  51.             foreach ($wikilinks[0] as $wikilink)  {
  52.                 $text = str_replace($wikilink, "[TOKEN]", $text);
  53.                 $wikilink = preg_replace("/(\#)|(\#)/", "", $wikilink);
  54.                 $url_data = preg_split("/[\s,]+/", $wikilink, 2);
  55.                 $href = trim($url_data[0]);
  56.                 $linkName = "Click Here";
  57.                 if (isset($url_data[1])) {
  58.                     $linkName = trim($url_data[1]);
  59.                 }
  60.  
  61.                 $anchor = "<a target='_blank' href='".$href."'>".$linkName."</a>";
  62.                 $text = str_replace("[TOKEN]", $anchor, $text);
  63.             }
  64.         }
  65.         return $text;
  66.     }
  67. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement