?
Solved

BB Code Replacement Help

Posted on 2004-11-19
4
Medium Priority
?
353 Views
Last Modified: 2006-11-17
Hi,

I am making a forum and want to include BB Code in it.
So far it replaces BB Code like [b]text[/b] and [url]http://www.google.com[/url]
But I want it to replace BB Code like [url=http://www.google.com]Google[/url]

The code I have so far is:

====================PHP==================
if ($Bb_Code_Row['Advanced_Replace'] > "") {
                  $Post_Main_Text = str_replace($Bb_Code_Row['Open_Code'] . ".]", $Bb_Code_Row['Open_Replace'] . "$1" . $Bb_Code_Row['Advanced_Replace'], $Post_Main_Text);
                  $Post_Main_Text = str_replace($Bb_Code_Row['Close_Code'], $Bb_Code_Row['Close_Replace'], $Post_Main_Text);
            } else {
                  $Post_Main_Text = str_replace($Bb_Code_Row['Open_Code'], $Bb_Code_Row['Open_Replace'], $Post_Main_Text);
                  $Post_Main_Text = str_replace($Bb_Code_Row['Close_Code'], $Bb_Code_Row['Close_Replace'], $Post_Main_Text);
            }
===================PHP===================

The html output for the advanced BB Code (being [url= ] [/url]) is (using Google as an example):

<a href='http://www.google.com]Google</a>

If I replace ] with '> it will replace it for all other BB Codes (e.g. [b]text[/b] would become <b'>text</b'>) which obviously would cause problems.

I've thought about doing it with a pattern, but can't work out how.

Can anyone help me please?

Thanks In Advance
mms_master
0
Comment
Question by:mms_master
  • 3
4 Comments
 
LVL 1

Accepted Solution

by:
ryan-h earned 80 total points
ID: 12625188
<?
$bb = "[url=http://www.google.com]Google[/url]";
$out = preg_match("/\[url=(.*)\](.*)\[\/url\]/", $bb, $m);
$href = "<a href='".$m[1]."'>".$m[2]."</a>";

print $href;

?>
0
 
LVL 5

Author Comment

by:mms_master
ID: 12625372
the only problem with that is it takes the url out of the string and sets it as $href.

I need my code to keep the url in $Post_Main_Text, because otherwise I have to seperate it out to put $href back in the right part of the post.
0
 
LVL 5

Author Comment

by:mms_master
ID: 12630498
If it isn't possible to keep it in $Post_Main_Text can you tell me how to replace it and then sepereate the string and post the replacementin the right place.

Or if there is another way to do it then could you tell me please?

The strings will be coming from a database and the code I need will be in a while loop.
0
 
LVL 5

Author Comment

by:mms_master
ID: 12634360
I searched on google, found some code and modified it.

It's similar to what you said but using pred_replace() instead of preg_match().

Thanks anyway for the help and scene as you gave me an idea of what I was looking for i'll give you the points.

mms_master
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question