class parser
{
//html = true
//autorise de metre du html
//html = false
//interdit l'usage de html
function parse($txt, $html=false)
{
$txt=htmlentities($txt, ENT_QUOTES);
$img = array(
':)' => 's01.gif',
':-)' => 's01.gif',
':p' => 's02.gif',
'??' => 's03.gif',
'???' => 's03.gif',
'!!' => 's04.gif',
'!!!' => 's04.gif',
':garde:' => 's05.gif',
'^_^' => 's06.gif',
'^^' => 's06.gif',
':><:' => 's07.gif',
':chevalier:' => 's08.gif',
';)' => 's09.gif',
';o)' => 's09.gif',
':|' => 's10.gif',
':-°' => 's11.gif',
':-*' => 's11.gif',
':(' => 's12.gif',
':|' => 's10.gif',
);
$txt=preg_replace("/\[b\](.*?)\[\/b\]/si","$1",$txt);
$txt=preg_replace("/\[i\](.*?)\[\/i\]/si","$1",$txt);
$txt = preg_replace( "#\[url\](\S+?)\[/url\]#ie" , "parser::regex_build_url(array('html' => '\\1', 'show' => '\\1'))", $txt );
$txt = preg_replace( "#\[url\s*=\s*\"\;\s*(\S+?)\s*\"\;\s*\](.*?)\[\/url\]#ie" , "parser::regex_build_url(array('html' => '\\1', 'show' => '\\2'))", $txt );
$txt = preg_replace( "#\[url\s*=\s*(\S+?)\s*\](.*?)\[\/url\]#ie" , "parser::regex_build_url(array('html' => '\\1', 'show' => '\\2'))", $txt );
while( preg_match( "#\n?\[list\](.+?)\[/list\]\n?#ies" , $txt ) )
{
$txt = preg_replace( "#\n?\[list\](.+?)\[/list\]\n?#ies", "parser::regex_list('\\1')" , $txt );
}
while( preg_match( "#\n?\[list=(a|A|i|I|1)\](.+?)\[/list\]\n?#ies" , $txt ) )
{
$txt = preg_replace( "#\n?\[list=(a|A|i|I|1)\](.+?)\[/list\]\n?#ies", "parser::regex_list('\\2','\\1')" , $txt );
}
foreach ( $img as $key=>$contenu )
{
$txt = str_replace($key,'
',$txt);
}
$txt=nl2br($txt);
return $txt;
}
function unparse($txt)
{
$txt=preg_replace("/(.*)\<\/strong>/si","[b]$1[/b]",$txt);
$txt=preg_replace("/(.*)\<\/em>/si","[i]$1[/i]",$txt);
$txt = preg_replace( "#(\n){0,}#" , "\\1\[LIST\]" , $txt );
$txt = preg_replace( "#(\n){0,}#" , "\\1\[LIST=\\2\]\n" , $txt );
$txt = preg_replace( "#(\n){0,}- #" , "\n\[*\]" , $txt );
$txt = preg_replace( "#(\n){0,}
(\n){0,}#", "\n\[/LIST\]\\2" , $txt );
$txt = preg_replace( "#(\n){0,}(\n){0,}#", "\n\[/LIST\]\\2" , $txt );
$txt = preg_replace( "#(.+?)#" , "\[URL=\\1\\2\]\\3\[/URL\]" , $txt );
$txt = str_replace( "", "", $txt );
$txt=str_replace("
","",$txt);
$txt=str_replace("\n\n","",$txt);
//$txt=str_replace("\\'","'",$txt);
$img = array(
':)' => 's01.gif',
':-)' => 's01.gif',
':p' => 's02.gif',
'??' => 's03.gif',
'???' => 's03.gif',
'!!' => 's04.gif',
'!!!' => 's04.gif',
':garde:' => 's05.gif',
'^_^' => 's06.gif',
'^^' => 's06.gif',
':><:' => 's07.gif',
':chevalier:' => 's08.gif',
';)' => 's09.gif',
';o)' => 's09.gif',
':|' => 's10.gif',
':-°' => 's11.gif',
':-*' => 's11.gif',
':|' => 's12.gif',
);
foreach ( $img as $key=>$contenu )
{
$txt = str_replace('
',$key,$txt);
}
return trim(stripslashes($txt));
}
function regex_build_url($url=array()) {
$skip_it = 0;
// Make sure the last character isn't punctuation.. if it is, remove it and add it to the
// end array
if ( preg_match( "/([\.,\?]|!)$/", $url['html'], $match) )
{
$url['end'] .= $match[1];
$url['html'] = preg_replace( "/([\.,\?]|!)$/", "", $url['html'] );
$url['show'] = preg_replace( "/([\.,\?]|!)$/", "", $url['show'] );
}
// Make sure it's not being used in a closing code/quote/html or sql block
if (preg_match( "/\[\/(html|quote|code|sql)/i", $url['html']) )
{
return $url['html'];
}
// clean up the ampersands
$url['html'] = preg_replace( "/&/" , "&" , $url['html'] );
// Make sure we don't have a JS link
$url['html'] = preg_replace( "/javascript:/i", "java script: ", $url['html'] );
// Do we have http:// at the front?
if ( ! preg_match("#^(http|news|https|ftp|aim)://#", $url['html'] ) )
{
$url['html'] = 'http://'.$url['html'];
}
//-------------------------
// Tidy up the viewable URL
//-------------------------
if (preg_match( "/^
".$show."" . $url['end'];
}
function regex_list( $txt="", $type="" )
{
if ($txt == "")
{
return;
}
//$txt = str_replace( "\n", "", str_replace( "\r\n", "\n", $txt ) );
if ( $type == "" )
{
// Unordered list.
return "".parser::regex_list_item($txt)."
";
}
else
{
return "".parser::regex_list_item($txt)."
";
}
}
function regex_list_item($txt)
{
$txt = preg_replace( "#\[\*\]#", "" , trim($txt) );
$txt = preg_replace( "#^?li>#" , "", $txt );
return str_replace( "\n", "", $txt."" );
}
}
?>