Author Topic: Replace BbCode [Font]  (Read 578 times)

0 Members and 1 Guest are viewing this topic.

Offline stitchmediaTopic starter

  • Irregular
  • Posts: 8
    • View Profile
Replace BbCode [Font]
« on: March 11, 2010, 06:07:03 AM »
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 [FONT] 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 [URL] tag.

Thanks
Dave

Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: Replace BbCode [Font]
« Reply #1 on: March 11, 2010, 07:19:13 AM »
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.
CodeCanyon - Cheap, High Quality, Ready Made Scripts.

Offline stitchmediaTopic starter

  • Irregular
  • Posts: 8
    • View Profile
Re: Replace BbCode [Font]
« Reply #2 on: March 11, 2010, 07:35:26 AM »
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..

Code: [Select]
[font=Arial]value[/font]needs changing to something like...
Code: [Select]
<span style="font:Arial">value</span>
and

Code: [Select]
[url=http://www.google.com]value[/url]needs changing to...
Code: [Select]
<a href="http://www.google.com">value</a>

Offline XeNoMoRpH1030

  • Enthusiast
  • Posts: 71
    • View Profile
    • Blue Line Attack
Re: Replace BbCode [Font]
« Reply #3 on: March 11, 2010, 07:42:20 AM »
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
Code: [Select]
[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);

Offline cags

  • Guru
  • Fanatic
  • *
  • Posts: 3,250
  • Gender: Male
    • View Profile
    • TiB Studios
Re: Replace BbCode [Font]
« Reply #4 on: March 11, 2010, 07:42:45 AM »
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.

« Last Edit: March 11, 2010, 07:43:23 AM by cags »
CodeCanyon - Cheap, High Quality, Ready Made Scripts.

Offline stitchmediaTopic starter

  • Irregular
  • Posts: 8
    • View Profile
Re: Replace BbCode [Font]
« Reply #5 on: March 11, 2010, 07:53:15 AM »
Thanks very much for both your help, all sorted now  :D