<?php

				$str = "Je suis :)mais demain :D vient:)manger puis le chat:S    dort sur le lit";	


				$icons = array( ':)' , ':(' , ':P' , ':D', ':S', ':O', ':=)', ':|H', ':X',':-*');				

				$associative = array( 	':)'  => '<img src="images/chat/0.gif" />',
										':(' => '<img src="images/chat/1.gif" />',
										':P' => '<img src="images/chat/2.gif" />',
										':D' => '<img src="images/chat/3.gif" />',
										':S'  => '<img src="images/chat/4.gif" />',
										':O'  => '<img src="images/chat/5.gif" />',
										':=)' => '<img src="images/chat/6.gif" />',
										':|H' => '<img src="images/chat/7.gif" />',
										':X'  => '<img src="images/chat/8.gif" />',
										':-*' => '<img src="images/chat/9.gif" />');

				for($i = 0; $i < 9; $i++)
				{
					$val = $associative[$icons[$i]];
					$str = str_replace($icons[$i], $val , $str);
				}
				echo $str;
				
				
				//http://g-rossolini.developpez.com/tutoriels/php/regex/?page=fonctions
				//https://openclassrooms.com/courses/concevez-votre-site-web-avec-php-et-mysql/les-expressions-regulieres-partie-2-2-2
				
				
				//BBCode Parser PHP function:
				
				function showBBcodes($text) {

// BBcode array
$find = array(
'~\[b\](.*?)\[/b\]~s',
'~\[i\](.*?)\[/i\]~s',
'~\[u\](.*?)\[/u\]~s',
'~\[quote\](.*?)\[/quote\]~s',
'~\[size=(.*?)\](.*?)\[/size\]~s',
'~\[color=(.*?)\](.*?)\[/color\]~s',
'~\[url\]((?:ftp|https?)://.*?)\[/url\]~s',
'~\[img\](https?://.*?\.(?:jpg|jpeg|gif|png|bmp))\[/img\]~s'
);

// HTML tags to replace BBcode
$replace = array(
'<b>$1</b>',
'<i>$1</i>',
'<span style="text-decoration:underline;">$1</span>',
'<pre>$1</'.'pre>',
'<span style="font-size:$1px;">$2</span>',
'<span style="color:$1;">$2</span>',
'<a href="$1">$1</a>',
'<img src="$1" alt="" />'
);

// Replacing the BBcodes with corresponding HTML tags
return preg_replace($find,$replace,$text);
}

// How to use the above function:

$bbtext = "This is [b]bold[/b] and this is [u]underlined[/u] and this is in [i]italics[/i] with a [color=red] red color[/color]";
$htmltext = showBBcodes($bbtext);
echo $htmltext;

				
?>