PHPFront

Home > PHP > bb code link
The PHP website created by its users!



bb code link


Author
DescriptionReplaces [url=http://....]test[/link] with real html links.
RatingThis script has been rated 2/5 (5 votes)

PHP Code


function replaceUrl($message) {
   // Make link from [url]htp://.... [/url] or [url=http://.... ]text[/url]
   if (strpos($message, "[url")){
       $begUrl = strpos($message, "[url");
       $endUrl = strpos($message, "[/url]");
       $url = substr($message, $begUrl, $endUrl-$begUrl+6);
       $posBracket = strpos($url, "]");
  
       if ($posBracket != null){
           if ($posBracket == 4){ 
               // [url]http://.... [/url]
               $link        = substr($url, 5, $endUrl - $begUrl -5);
               $htmlUrl    = "<a href=$link target='_blank'>$link</A>";
           } else {           
               // [url=http://....]text[/url]
               $link        = substr($url, 5, $posBracket-5);
               $text        = substr($url, $posBracket+1, strpos($url, "[/url]") - $posBracket-1);
               $htmlUrl    = "<a href=$link target='main'>$text</A>";
           }
       }
              
       $message = str_replace($url, $htmlUrl, $message);
       // searches for other [url]-nodes
       $message = replaceUrl($message);
   }
   return $message;
}



Rate it: Print Print

Comments


No comments posted yet.

Name
Website
Your comment