PDA

View Full Version : php bbcode problem


xiaodao
11-27-2004, 06:33 AM
$bold = '/(\[B\])(.+)(\[\/B\])/';
$italic = '/(\[I\])(.+)(\[\/I\])/';

$str = preg_replace($bold,'<b>\\2</b>',$str);
$str = preg_replace($italic,'<i>\\2</i>',$str);

i discovered some code like these to insert bbcode into a form

but but, i am a newbie, i cannot figure out this , can anybody explain it, especially (.+) and \\2

Fou-Lu
11-27-2004, 08:52 AM
I was going to say the code looks fine, but you just need info on what it means eh?
Its a preg replacement pattern technique. Heres a breakdown:
. = Any character (Excluding newline)
^ = Begins with / Exclusion (when used with class definitions)
- = range withing class definitions.
$ = Ending with
\ = Escape character
[ and ] = class definitions
| = alternative branch (or)
? = 1 or none
* = 0 or more
+ = at least one
{ and } = quantifiers

\\{$i} = backreference
You will use this a lot as well:
i = case insensitive
m = multiline
s = dotall
x = extended
u = ungreedy

Lets see, the important one to learn with this is the backreference, which is your \\1 \\2 etc. That relates to where its located in your search as surrounded by brackets.
So take this:
/(<\/?)(\w+)([^>]*>)/e
replace with:
'\\1' . strtoupper('\\2') . '\\3'
What this will do is change all html markup found in a document to uppercase. \\1 is your first set of brackets, which should be a <, \\2 is your second set (\w+) which will be your markup letter/word and \\3 is your close bracket.
This was taken directly from php.ca, though to me it looks like its off already, go figure there are easier ways anyway.
But yeah, its important to learn replacement for shortcuts, though a lot of what you can do using preg/str/ereg replacement and matching is technically possible through other means.

xiaodao
11-27-2004, 09:13 AM
my god, i am totally confused...

xiaodao
11-27-2004, 09:18 AM
/(<\/?)(\w+)([^>]*>)/e

so what is w+ mean
/e?
/?
and ^>?
i am a newbie, please help me , thanks a lot

Fou-Lu
11-27-2004, 02:20 PM
Sorry, I missed quite a few I thought you wouldn't need, and low and behold my replacement has just that, lol.

w = any word character (alphanumeric, space and underscore).
The + is for any character thats alphanumeric (or underscore, though i doubt you would find that in html markup), any number of times.

The breakdown is as follows:
(<\/?)
This is saying look for an open (left) bracket, with the possibility of the ending deliminator (/). The first is to escape, as a standard / is needed to be escaped, as it is used for others such as your back reference.
(\w+)
Search for any number of word characters within. Including spaces, alphanumeric and underscore.
([^>]*>)
Search for any number of characters that do not begin with a right closed bracket, followed by a closing bracket.

So, html code subjected to this would uppercase the markup as is stated with the replacement techinique.
<b>
< search = (<\/?) = </
b search = (\w+)
> search = ([^>]*>)

Also, good reference, though few examples are at php.com and can be directly linked here:
http://ca3.php.net/manual/en/reference.pcre.pattern.syntax.php

xiaodao
11-27-2004, 03:55 PM
This is a great tutorial, thank you very much,