Re: Fw: [PHP] FORUM CODE

From: Date: Wed, 04 Sep 2002 07:43:43 +0000
Subject: Re: Fw: [PHP] FORUM CODE
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-115253@lists.php.net to get a copy of this message
Kevin Stone wrote:
$myformtxt = '[i]Hello[/i] [B]world[/B]'; // pretend this came from a form.
1. I understand what you're doing, and how. But what I don't understand is why? If a user is going to have to type [B]bold[/B], why not just get them to type <b>bold</b>. [bold] and [italic] maybe? 2, I think regular expressions are a safer way to go. That way you don't end up inserting eroneous html into peoples text if an unmatched tag is encountered. Square brackets will not throw a browser off track but an unclosed <h1> or <em> tag could ruin a page's appearance. i suggest using preg_replace, maybe like this:
$pair = array( 'bold'     => 'strong',
               'italics'   => 'em',
               'heading1' => 'h1',
               'heading2' => 'h2',
               'heading3' => 'h3',
               'heading4' => 'h4');
$pattern = array(); $replace = array(); foreach($pair as $key => $val) {
    $pattern[] = '/(\[)(' . $key . ')(\])([^]].*)(\[\/)(' . $key . ')(\])/i';
    $replace[] = "<$val>" . '$4' . "</$val>";
} $new_body= preg_replace ($pattern, $replace, $body_text);

« previous php.general (#115253) next »