Jump to content
xisto Community
Sign in to follow this  
DeveloperX

Regular Expressions best way to repace strings

Recommended Posts

Hello! I as all of You use this greatful FORUM. But I want to know how engine of forum replaces post like

[color=red]TEXT[/color]

to

<font color=red>TEXT</font>

But I think that some Bbcodes like
[b][/b]
maybe replaced by this method:

$str="[b]TEXT[/b]";$str=str_replace("[b]","<b>",$str);$str=str_replace("[/b]","</b>",$str);echo $str;

Please help me with URL, Email, Img, Color bbcodes!
Edited by DeveloperX (see edit history)

Share this post


Link to post
Share on other sites

Regular expressions are used, but it's a little more complex than that. If you have a look through the source code of IPB (or any other forum software that supports bbCode tags), you'll be able to get a much better example than anything I can provide.

Anyway, here's a quick example of how formatting could work (in theory, anyway):

<?php$post = preg_replace('/\[(\/)?(b|u|i)\]/ie', '"<$1" . strtoupper("$2") . ">"', $post);?>

[b]This[/b] is my [i]post[/i] and it [b][u]rocks[/u][/b].Becomes:<B>This</B> is my <I>post</I> and it <B><U>rocks</U></B>.

Share this post


Link to post
Share on other sites

Regular expressions are used, but it's a little more complex than that. If you have a look through the source code of IPB (or any other forum software that supports bbCode tags), you'll be able to get a much better example than anything I can provide.

 

Anyway, here's a quick example of how formatting could work (in theory, anyway):



Thanks!

And how about bbcodes???

Help...



Edited by OpaQue (see edit history)

Share this post


Link to post
Share on other sites

Correct me if i'm wrong, but Spectre's way wont work all that well. Cause it'll replace the tags whether or not they've ended or began the tags.

Bold:

$var = preg_replace('/\\[(b|i|u)[^\\]]*\\](.*?)\\[\/\\1\\]/', '<\\1>\\2</\\1>', $var);

Italic:
$var = preg_replace('/\\[(b|i|u)[^\\]]*\\](.*?)\\[\/\\1\\]/', '<\\1>\\2</\\1>', $var);

Underline:
$var = preg_replace('/\\[(b|i|u)[^\\]]*\\](.*?)\\[\/\\1\\]/', '<\\1>\\2</\\1>', $var);

Color:
$var = eregi_replace('\\[color="([^"]*)"\\]([^\\[]*)\\[/color\\]', '<font color="\\1">\\2</font>', $var);

Url:
$var = eregi_replace('\\[url="([^"]*)"\\]([^\\[]*)\\[/url\\]', '<a href="\\1" target="_blank">\\2</a>', $var);

Email:
$var = eregi_replace('\\[mail="([^"]*)"\\]([^\\[]*)\\[/mail\\]', '<a href="mailto:\\1">\\2</a>', $var);

Img:
$var = eregi_replace('\\[img\\]([^\\[]*)\\[/img\\]', '<img src="\\1" />', $var);

These should all work properly.

[edit]Yes, i noticed that too, Spectre, but i modified it now so it'll work if it were to be like that. The 3 multiples are required for the script to work, don't ask me why...[/edit]
Edited by truefusion (see edit history)

Share this post


Link to post
Share on other sites

Well, I don't know exactly how IPB does it (as I don't have the source code on hand), but something like this should do the trick:

$post = preg_replace('/\[url=http:\/\/(.[^\]]*)\](.[^\[]*)\[\/url\]/ie', '\'<a href="http://$1" target="_blank" title="\'.htmlentities(\'$2\').\'">\'.htmlentities(\'$2\').\'</a>\'', $post);

Share this post


Link to post
Share on other sites

truefusion, although you are right on it picking up unclosed (or unopened) tags, I don't think your way would work all that well either. Your method would skip over text which contains a [ character - for example, in the sample text I provided above, '[ b ][ u ]rocks[ / u ][ / b ]' would not be set as bold and italic, but only as italic.

Edited by Spectre (see edit history)

Share this post


Link to post
Share on other sites

Email:

$var = preg_replace('/\\[mail\\]([\\w\\._-]+@[\\w\\-_]+\\.[a-zA-Z]{2,4})\\[\/mail\\]/', '<a href="mailto:\\1">email</a>', $var);

Color:
$var = ereg_replace('\\[color="([\\w#]*)"\\]([^\\[]*)\\[/color\\]', '<font color="\\1">\\2</font>', $var);

Image:
$var = preg_replace('/\\[img\\](https?:\/\/[a-z.]{4,}\\w*\\.[a-z]{2,4}(?::\\\\d+)?\/[\\w\\-_\\d\/]*\\.(?:jpg|jpeg|png|gif))\\[\/img\\]/', '<img src="\\1" />', $var);

Edited by truefusion (see edit history)

Share this post


Link to post
Share on other sites

If ereg_replace() fails to work, switch to preg_replace(), and then modify the pattern for it to work with preg_replace():

$var = preg_replace('/\\[color="([\\w#]*)"\\]([^\\[]*)\\[\/color\\]/', '<font color="\\1">\\2</font>', $var);

Share this post


Link to post
Share on other sites

If ereg_replace() fails to work, switch to preg_replace(), and then modify the pattern for it to work with preg_replace():

$var = preg_replace('/\\[color="([\\w#]*)"\\]([^\\[]*)\\[\/color\\]/', '<font color="\\1">\\2</font>', $var);

I'm sorry, but your code not works again!
Maybe something wrong?
Please give me a workable code...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.