Jump to content

Replace BbCode [Font]


stitchmedia

Recommended Posts

I am new to this regex stuff so am still on a massive learning curve.  I have a file called bbcode.php which contains all of my regex to convert BbCode to HTML but I dont know how to convert the tag.

 

I have this line to convert the size:

$text = eregi_replace("\\[size([^\\[]*)\\]([^\\[]*)\\[/size\\]","<font size=\"\\1px\">\\2</font>",$text);

 

I could start playing around with it but I really dont know what I am doing until I have read up more about it.  I also need to know how to convert the tag.

 

Thanks

Dave

Link to comment
https://forums.phpfreaks.com/topic/194887-replace-bbcode-font/
Share on other sites

Firstly the eregi functions are deprecated, you should consider swapping over to use the preg functions instead. Secondly, how exactly conversion of the Font and URL tags would work is very much dependent on how exactly you use the BBCode tags, I've seen multiple different techniques for using them. If you want help with Regex patterns you really need to provide example inputs, expected outputs and any tricky case that may confuse a normal pattern match.

Link to comment
https://forums.phpfreaks.com/topic/194887-replace-bbcode-font/#findComment-1024712
Share on other sites

Thanks for the reply.  The regex I got as an example from somewhere else but I will try and update as you suggest.

 

Examples are..

 

[font=Arial]value[/font]

needs changing to something like...

<span style="font:Arial">value</span>

 

and

 

[url=http://www.google.com]value[/url]

needs changing to...

<a href="http://www.google.com">value</a>

Link to comment
https://forums.phpfreaks.com/topic/194887-replace-bbcode-font/#findComment-1024720
Share on other sites

If that is the case, something like this would work:

 

$text = preg_replace('/\[font\=(.*?)\](.*?)\[\/font\]/is', '<span style="font: $1;">$2</span>', $text);

 

Your example would work, but also

[font=15px arial,sans-serif]Test[/font]

would work.

 

For the URL, you can get really specific, but one I use is

$text = preg_replace('/\[url\=(.*?)\](.*?)\[\/url\]/is','<a href="$1">$2</a>',$text);

Link to comment
https://forums.phpfreaks.com/topic/194887-replace-bbcode-font/#findComment-1024723
Share on other sites

There's probably several dozen examples of the url tags if you search the Regex board but...

 

$input = '[font=Arial]value[/font]';
$pattern = '#\[font=([^]]+)]([^[]+)\[/font]#i';
$replacement = '<span style="font:$1">$2</span>';

echo preg_replace( $pattern, $replacement, $input );

$input = '[url=http://www.google.com]value[/url]';
$pattern = '#\[url=http://([^]]+)]([^[]+)\[/url]#i';
$replacement = '<a href="$1">$2</a>';

echo preg_replace( $pattern, $replacement, $input );

Edit: Beaten to the punch.

 

 

Link to comment
https://forums.phpfreaks.com/topic/194887-replace-bbcode-font/#findComment-1024724
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.