Username:
Password:
Remember me:
Register

Back to forum: Web Programming (HTML, JS, CSS, PHP, MySQL)


Search forums via Google


1 Users appreciate this thread.

HowTo: Create BBCodes in PHP (with PCRE/preg)
 >  >>
Started by SomeLuigi
(2012-05-29 19:34:27)
SomeLuigi (2012-05-29 19:34:27)
Ladies and gentlemen! A quick Perl-Compatible Regular Expressions (PCRE) lesson integrated with PHP: Hypertext Preprocessor to create a BBCode system!

Let's get started!

First, imagine we already have a Profile system set up!

Let's first create a new function called BBCode_translate()



<?php

function BBCode_translate($psource) {

};
?>


Now we need to explore PCRE a little bit...

We will be using PHP's function preg_replace(PATTERN, RESULT, SOURCE)


First let's look at an example line of code:


$psource = preg_replace('%\[u\](.+)\[/u\]%iUs', '<u>$1</u>', $psource);


Now you are feeling confused. Let's start off with some basics...

A pattern in PCRE is contained within DELIMITERS.
These must be escaped with a backslash if put in the pattern itself, however. The easy solution is to use a percent sign. It can be any symbol that isn't reserved. I recommend '%'.

So, our pattern looks like this:

%%

next after the delimiters, we place FLAGS.

%%iUs

i- case-insensitive: matches uppercase and lowercase

U- UNGREEDY, a.k.a. unique (must be cap) Makes .* not be so greedy.

s- DOTALL dot matches everything, instead of not allowing newlines.


Now we get to the real deal - the actual pattern itself.


Let's get some things straight first.

(.*) will match some text and place it in a 'backtrack variable' - we'll get on to this later.

(yes|no) will allow only yes or no, and also place it in a backtrack variable.

(.) will select one character and backtrack it

.{1,3} will match between 1 and 3 characters of any kind.

[a-zA-Z] will match 1 alphabetical character

[a-zA-Z]{0,} will match all alphabetical characters

[0-9]{1,3} will allow a number between one and three digits.

Remember most can be taken to a backtrack variable later!


Now I will talk about ESCAPING

If a character is special in PCRE, you will need to place a backslash (\) in front of it.

\\ egates to \
\. egates to .
\s egates to ANY WHITESPACE CHARACTER


And also,
^ will trigger at the start of a string/line
$ at the end of one.


Now that we know that, let's get cracking!

First let's create [u]!

The syntax for this BBCode looks something like this in our memory:

BBCode
[u]some text to underline[/u]

HTML
<u>some text to underline</u>

First I'll tell you that [ and ] need to be escaped.

So, [u] looks like \[u\]

Now we need to capture the text inbetween:
(.+)

and then the ending tag [/u] looks like:
\[/u\]


So,

\[u\](.+)\[/u\]

then the delimiters and flags:

%\[u\](.+)\[/u\]%iUs

Now we need to get the captured text and use it in our HTML pattern:

<u>$1</u>

1 is the number of the capture, so you could add another capture and use it as number 2.

So, our line of code looks like this:

$psource = preg_replace('%\[u\](.+)\[/u\]%iUs', '<u>$1</u>', $psource);


Let's remember our function from earlier and add what is necessary:


<?php

function BBCode_translate($psource) {
$psource = preg_replace('%\[u\](.+)\[/u\]%iUs', '<u>$1</u>', $psource);
};
?>


Now one problem still: users can type in HTML. To prevent this, we can filter with htmlentities()

and then return the result


<?php

function BBCode_translate($psource) {
$psource = htmlentities($psource);
$psource = preg_replace('%\[u\](.+)\[/u\]%iUs', '<u>$1</u>', $psource);
return $psource;
};
?>



Now place this in a file, include it with include_once(PATH TO THE PHP FILE WITH THE FUNCTION IN IT), then do something like this:

$text = BBCode_translate($text);




This post has been edited one or more times, the last time was:
2012-05-29 19:35:25

SomeLuigi is changing in 2015.
roxasmaker (2012-05-29 23:41:48)
e3e so much...

ps:Can i copy and paste those

pss:Will it work on x90x.net?
Sup, I'm here!

Follow me on Twitter: @r0xasmaker
Junaid (2012-05-30 20:15:03)
Thanks for making this.
I was using str_replace($function1,$function2,$something); with function1 and function2 being an array of commands and the results, but now I know the proper way of implementing BBCodes. -b

Edit:
How would I make <br> equivalent to if someone pushed enter or moved onto a new line?

This post has been edited one or more times, the last time was:
2012-05-30 20:47:28

SomeLuigi (2012-06-14 20:00:38)
-Juny use nl2br()

in non-XHTML environments, you will need to replace <br/> with <br> after.
SomeLuigi is changing in 2015.
BioMan8 (2012-06-18 02:59:02)
Wow, this is how you make emoticons work...
11Drago (2012-06-25 00:02:31)
This is confusing.
11Drago (2012-06-27 14:47:16)
I know complete PHP. o-o
SPCOxion (2012-07-31 05:30:54)
o.o Uh...
Time to finish CSS, learn JavaScript, and learn PHP!

This post has been edited one or more times, the last time was:
2012-07-31 05:31:37

SPCOxion (2012-07-31 12:07:10)
@Abranda: I'd suggest LittleWebHut, but they don't have a JS guide (though it would be very well-explained, thus why I'd reccomend it).

I think I've got it. I have

<?php

function BBCode_translate($psource){
$psource = htmlentities($psource);
$psource = preg_replace('%\[b\](.+)\[/b\]%iUs', '<b>$1</b>', $psource);
return $psource;
};

?>

But it won't work.
I'm using the code above in one file (bbcode_function.php), and I'm trying to test the code in another(bbcode.php).
What do I put in bbcode.php?

I did this without knowing any PHP whatsoever, soo... d:

EDIT: Nevermind the error. I didn't even link to bbcode_function.php .
I'll do this stuff when I know more PHP.

This post has been edited one or more times, the last time was:
2012-08-21 11:27:51

Fayne_Aldan (2012-08-21 09:04:17)
Proxion, here's the code you should use in bbcode.php:

<?php
include_once("bbcode_function.php";
$(variable) = BBCode_translate("Text here)";
?>

Then you have to do something with the variable that you entered. Like, if the variable is $a, try:

<?php
echo $a;
?>
djl12328 (2012-08-21 20:15:20)
Thank you SL. I just got an almost complete BBCode engine working. I just need emoticons now.
www.dark-isle.weebly.com
(Capt.)Orb (2012-08-26 00:42:36)
Very very useful thanks!
Aviation fact: The a380 is the largest commerical aircraft in service!

Go to Splashiverse! Splashiverse
SolidSnake (2012-09-18 04:51:43)
Cool -b

This post has been edited one or more times, the last time was:
2012-09-18 04:52:23

SuperShadic76 (2013-01-21 22:54:39)
Im still confused
Hey im on ROBLOX add me!Its manicalgogetassj4!Im cool with swag!

Also check out my EPIC profile!

[IMG]http://i357.photobucket.com/albums/oo14/Nokrow889/930297907_s.gif[/IMG]
wellsandlava (2013-11-06 06:08:13)
THANK U SO MUCH!
This account is under a new owner
 >  >>

Log in to submit a comment

This topic's ID: 17503

Back to forum: Web Programming (HTML, JS, CSS, PHP, MySQL)




Total registered users: 8321
New registered users today: 7
Newest registered user: ElegantVulpes

©  Copyright 2026 3DSPlaza. All Rights Reserved