<?php

function smarty($data){
 
$data =  stripslashes($data);
 
$data preg_replace('/(["\']+?)([\.,]+?)/',"$2$1",     $data);
 
$data preg_replace('/"(.+?)"/si'"&#8220;$1&#8221;"$data);
 
$data preg_replace("/(\b\w+)'(\w+\b)/" "$1&#8217;$2",  $data);
 
$data preg_replace("/'(.+?)'/si""&#8216;$1&#8217;"$data);
 
$data preg_replace('/"(.+?)\'/si',"&#8220;$1&#8221;"$data);
 
$data preg_replace('/\'(.+?)"/si',"&#8216;$1&#8217;"$data);
 
$data preg_replace("/(\w+)\*+(\w+)/i","$1&#8212;$2" $data);
 
$data =  str_replace("...","&#8230;",$data);
 
$data =  (stristr($_SERVER["HTTP_USER_AGENT"],"Gecko"))?str_replace(".""&#8228;"$data):           $data;
 
$data =  str_replace("--","&#8212;"$data);
 
$data =  str_replace("-""&#8211;"$data);

 
preg_match_all("/<(.*?)>/"$data$out);
 for(
$i=1$i<count($out); $i++){
    
$out[$i] = str_replace("/""\\/"$out[$i]);
 }
 
$x=0;
  for(
$i=0$i<=count($out[0]); $i++){
 
$output  str_replace("&#8220;","\""$out[0]);
 
$output  str_replace("&#8221;","\""$output);
 
$output  str_replace("&#8228;","." $output);
 
$output  str_replace("&#8211;","-"$output);
 
$data = @preg_replace("/<".$out[1][$x].">(.*?)<".$out[1][$x+1].">/"$output[$x]."$1".$output[$x+1], $data);
 
$x+=2;
}
 return 
$data;
}

echo( 
  
smarty("<p>Um. Let's <a href=\"http://yah.com/\" title=\"Blah\">\"keep\"</a> it clean.
         </p>
         <p><a href=\"http://www.blah.com/\">\"<acronym title=\"blah\">Blah</acronym>\"</a></p>
         <p>Let's test this thing. That's right, we're testing.</p>
         <p>Now let's, 'Quote someone.'</p>
         <p>\"'Let's try again,' he said,\" she said.</p>"
)
 );

?>