<?php

function bb_code ($message){
	
//meklejam konstrukciju [url]http://www.saite.lv[/url]
preg_match_all("#\[url\](http://)?(.*?)\[/url\]#si",$message,$simple_bb_url_matches);

//meklejam konstrukciju [url=title]http://www.saite.lv[/url]
preg_match_all("#\[url=(http://)?(.*?)\](.*?)\[/url\]#si",$message,$with_title_bb_url_matches);

//meklejam konstrukciju [img]http://bilde[/img]
preg_match_all("#\[img\](http://)?(.*?)\[/img\]#si",$message,$simple_bb_img_matches);

//meklejam vienkarshi url
preg_match_all("/((ht|f)tps?:\/\/([\w\.]+\.)?[\w-]+(\.[a-zA-Z]{2,4})?[^\s\r\n\(\) \ <>\,\!]+)/si", $message, $simple_url);

if($simple_bb_url_matches[0][0]) 
	{
		$message = preg_replace("#\[url\](http://)?(.*?)\[/url\]#si", "<a href=\"http://\\2\" target=\"_blank\" class=\"postlink\">\\2</a>", $message);
	}
if($with_title_bb_url_matches[0][0]) 
	{
		$message = preg_replace("#\[url=(http://)?(.*?)\](.*?)\[/url\]#si", "<a href=\"http://\\2\" target=\"_blank\" class=\"postlink\">\\3</a>", $message);	
	}
	
if($simple_bb_img_matches[0][0]) 
	{
		$message = preg_replace("#\[img\](http:\/\/[^\(\) \t\n\r].*?)\[/img\]#si", "<div class=\"picinforum\"><img src=\"\\1\" border=\"0\" style=\"cursor: pointer;\" onClick=\" getImgSize(this.src);\"></div>", $message);	
	}
	
/* Tipo ar smailijiem ne jauna rinda, bet kaut kas gjuko ar bildem forumaa
if($simple_bb_img_matches[0][0]) 
	{
		$message = preg_replace("#\[img\](http:\/\/[^\(\) \t\n\r].*?)\[/img\]#si", "<img src=\"\\1\" border=\"0\" style=\"cursor: pointer;\" onClick=\" getImgSize(this.src);\">", $message);	
		if(!stristr($message,".gif")) {$message = str_replace("<img src=","<div class=\"picinforum\"><img src=",$message)."</div>";}
	}
	*/
	
foreach($simple_url[0] as $urlfound) {
	if(!stristr($urlfound,"[/url]"))
		{
			$maxurl_len = 65;
			$target = "_blank";
			
			$offset1 = ceil(0.75 * $maxurl_len) - 2;
			$offset2 = ceil(0.25 * $maxurl_len) - 1;
			
					if ($maxurl_len AND strlen($urlfound) > $maxurl_len)
						{
							$urltext = substr($urlfound, 0, $offset1) . "..." . substr($urlfound, -$offset2);
						}
					else
						{
							$urltext = $urlfound;
						}

							$message = str_replace($urlfound, '<a href="'. $urlfound .'" target="'. $target .'" title="'. $urlfound .'" class="postlink">'. $urltext .'</a>', $message);
						
		}
	}
	
// [quote]
$message = preg_replace("#\[quote\](.*?)#si", '<blockquote style="border: 1px solid black;" class="quoting">\\1', $message);				
$message = preg_replace("#(.*?)\[/quote]#si", "\\1</blockquote>", $message);
//[b]
$message = preg_replace("#\[b\](.*?)\[/b\]#si", "<b>\\1</b>", $message);
//[i]
$message = preg_replace("#\[i\](.*?)\[/i\]#si", "<i>\\1</i>", $message);
// [img]image_url_here[/img] code..
$message = preg_replace("#\[img\](http:\/\/[^\(\) \t\n\r].*?)\[/img\]#si", "<div class=\"imgbody\"><IMG src=\"\\1\" border=\"0\"></div>", $message);

return $message;
}

//message
$message = "

[url]http://www.simplesaite.lv[/url] 

[url=www.bmwpower.lv]BMWPOWER[/url]

http://www.vienakrshiurl.lv

http://kaka.lv 

[url]http://www.kautkas.lv[/url]

[url=www.majas.lv]majas[/url]

http://www.bmwpower.lv/placis_sludinajums.php?id=15

http://www.bmwpower.lv/placis_sludinajums.php?id=15

http://www.bmwpower.lv/placis.php

[IMG]http://i22.ebayimg.com/02/m/000/7f/dc/a781_27.JPG[/IMG]

[IMG]http://i8.ebayimg.com/02/m/000/7f/dc/ace3_27.JPG[/IMG]

";

echo nl2br(bb_code($message));

?>