Re: Re: BBCode?
| From: | Jason Wong | Date: | Mon, 25 Nov 2002 06:45:00 +0000 |
| Subject: | Re: Re: BBCode? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-125889@lists.php.net to get a copy of this message | ||
On Monday 25 November 2002 14:20, Kyle Gibson wrote:
> Leif K-Brooks wrote:
> > I'm working on adding simple BBCode to my site. I'm currently using the
> > [i] tag for testing, with the following code:
> >
> > <?php
> > function bbcode($text){
> > $text =
> > ereg_replace('\\[i\\](.{1,})\\[/i\\]','<i>\\1</i>',$text);
> > return $text;
> > }
> > print bbcode('[i]This[/i] is a [i]test[/i].');
> > ?>
> >
> > But it prints "<i>This[/i] is a [i]test</i>". Is there a better way
> > to
> > do this?
It's not working because it's doing a greedy match. Try (I prefer the PCRE
functions over EREG):
$text = preg_replace("/\[i\](.*)\[\/i\]/U", "<i>$1</i>", $text);
> <?
> function bbcode($text)
> {
> $text = str_replace("[","<",$text);
> $text = str_replace("]",">",$text);
> return $text;
> }
>
> print bbcode("[i]This[/i] is a [i]test[/i].");
>
> ?>
This code is simply not suitable. The object is to replace tags as an entity
as opposed to simply replacing '[' with '<'.