Re: Regular Expression problem
| From: | Monte Ohrt | Date: | Fri, 10 Nov 2000 22:17:12 +0000 |
| Subject: | Re: Regular Expression problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-24728@lists.php.net to get a copy of this message | ||
Hi Ilja,
You could probably figure out some complex regular expression to solve
this particular problem, although this may not be as quick or as safe as
breaking things down into managable parts. Maybe you could first search
and replace your static tags with str_replace, like so:
$bbcode = str_replace("[i]","<i>",$bbcode);
$bbcode = str_replace("[/i]","</i>",$bbcode);
$bbcode = str_replace("[/url]","</a>",$bbcode);
.... (etc, all of your static tags)
str_replace() is much faster than regular expression matching, so only
use ereg_* where absolutely necessary. So after you do this, you are
left with your dynamic tags, such as [url=...]
example:
[url=<link>]<i><hyperlink></i></a>
Now go ahead with your ereg_replace for hrefs:
$bbcode = ereg_replace("\[url=([^\]]+)\]","<a href=\\1>",$bbcode);
... and so on for any other dynamic tags
Monte
Ilja Hehenkamp wrote:
>
> Hi,
>
> I'm stuck within a regex problem,
>
> I know you can match charachters until a character you DON'T want to match,
> something like:
>
> [^#]*
>
> matches all character until a #
>
> but what when you wanna list until a whole WORD?
>
> Here's my example:
>
> $bbcode=eregi_replace("(\[size=)([^]]*)(])([^[/size\]]*)(\[/size\])", "<font
> size=\\2>\\4</font>",$bbcode);
>
> This searches for [url=<LINK>]<HYPERLINK>[/url] and parses it to <a
> href="<LINK>"><HYPERLINK></a>
>
> the ([^]]*) part is to avoid greediness but now he doesn't parse things
> like: [url=<link>][i]<hyperlink>[/i][/url] which is logical because he has
> to quit when he meets a '['
>
> What I want to do that he only stops when he finds: [/url], but [^[/url]] or
> [^\[/url\]] doesn't work....
>
> Anyone understands what I mean and knows the answer?
>
> Thanks!
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net