Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.