[Tutorial] Pattern Syntax

Example of PHP code.

Moderator: Moderators

[Tutorial] Pattern Syntax

Postby Phantom on Sun Jun 05, 2005 4:06 pm

If you have ever taken a look at includes/bbcode.php, you'll see that there is a function called preg_replace() with a bunch of random characters. Well, I'm here to help you out. Let's start with a real-life example. If you would go to line 283 of includes/bbcode.php, you'll see a code like this:

Code: Select all
preg_replace("#\[size=([1-2]?[0-9])\](.*?)\[/size\]#si", "[size=\\1:$uid]\\2[/size:$uid]", $text);

Let's start off with #\[size=([1-2]?[0-9])\](.*?)\[/size\]#si, please pay close attention since this uses many characters. The #\ is used so the server can look for a bracket, because many of these characters have multiple meanings. The [size= is the first part of the BBcode. Now for the ([1-2]?[0-9]), this tells the function what will be included in the next part of the BBcode will be between 1-2 and 0-9. The ( begins the subpattern, and the brackets with the numbers tell us that it would be between 1-2 and 0-9. The ? is another character for - since we cannot use - twice in a row. Then, once again, we have a backslash for \] so we can use the end bracket. The (.*?) tells us that there can be any character used more than once. Then we have our backslash for the bracket \[, and the rest of our bbcode. Next we have #si, which tells us that we can have a lower or upper case letter (s) or a period (i). Now, what we just covered was what the function will be looking for in $text. Let's go over what it should be replaced with:

In the next part of the function, we have [size=\\1:$uid]\\2[/size:$uid]. As you know, [size= is the beginning of our BBcode. \\1 is sort of like a $_POST variable. Whatever was there when the function was searching will be included once again in the replacement. So if you had say "6", it would use "6" again. Then for :$uid, we have a unique id for the message (determined by make_bbcode_uid() on line 238). Now we have another \\2. This is just another variable, but under a different name. Next we have the rest of our variable and the unique id. There you have it, we just went over some complicated stuff. Now I will list the majority of the characters you may find in preg_replace:

. - Any character (number, letter, etc.).
^ - The beginning of a line.
$ - The end of a line.
a? - The letter "a" is only mentioned once, or not at all (0-1).
a* - The letter "a" is mentioned at least once, or not at all (0 - 1+).
a{2} - The letter "a" is mentioned exactly two times.
a{2,} - The letter "a" is mentioned at least two times.
a{2,9} - The letter "a" is mentioned between 2 and 9, and including 2 and 9 (2-9).
[0-10] - Any number between 0 and 9 (0-9).
[a-f] - Any lowercase letter between "a" and "f" (a-f).
[A-F] - Any uppercase letter between "A" and "F" (A-F).
() - Groups expressions, ex: (b|c) - Between "b" and "c"
| - Used as an "or" statement
? - Used with () to extend its meaning, also equivalent to {0,1}
\ - Character used for many uses

For a complete list of characters, go to PHP's Pattern Syntax Manual and PHP's Pattern Modifiers Manual.

I am sorry if this tutorial was confusing for you, but if you would point out the places where I should expound on, that would make it much easier for people to learn. I do hope this tutorial was beneficial. Happy coding.

// Phantom
"Best of the best of the best, sir! With honors!" -- MIB
[ jphantom.com ]
Phantom
Registered User
Registered User
 
Posts: 245
Joined: Mon Apr 18, 2005 3:24 am

Postby PostBot on Sun Jun 05, 2005 4:15 pm

Nice tutorials. Thanks for posting it! :)
Do NOT pm me, I don't visit this forum anymore, don't own it, don't provide any support and don't moderate.
User avatar
PostBot
Moderator
Moderator
 
Posts: 10659
Joined: Sat Aug 02, 2003 3:52 pm
Location: Mars

Postby Phantom on Sun Jun 05, 2005 4:25 pm

Not a problem. As I learn more, you can be sure I will post new tutorials. =)
"Best of the best of the best, sir! With honors!" -- MIB
[ jphantom.com ]
Phantom
Registered User
Registered User
 
Posts: 245
Joined: Mon Apr 18, 2005 3:24 am

Postby ewelin on Tue Jun 28, 2005 6:22 pm

Wow... great tutorial!!!! thanks for typing this up... definitely helped me out!!!
ewelin
Registered User
Registered User
 
Posts: 679
Joined: Thu Apr 14, 2005 8:16 pm


Return to PHP Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron