[Tutorial] Regular Expressions

Example of PHP code.

Moderator: Moderators

[Tutorial] Regular Expressions

Postby Phantom on Sun Jun 05, 2005 1:57 pm

In this tutorial, we will be expounding on ereg() and str_replace() as our examples, but you may also use ereg_replace(), split(), eregi(), eregi_replace(), and spliti(). One thing to remember when using eregi, spliti, and other regular expressions that end with "i" are that they are case-sensitive. Now on to the tutorial:

There are numerous syntaxes that you use when setting up a regular expression, I will list them all, along with a brief description:
  • . - 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"

Now that we got that long list out of the way, let's use some examples:

Code: Select all
ereg("abcd", $text);

This line of code will return TRUE if abcd is found in the variable $text.

Code: Select all
ereg("^abcd", $text);

This line of code will return TRUE if abcd is found at the beginning of variable $text.

Code: Select all
ereg_replace("\n", "<br />", $text);

This line of code acts the same as nl2br() and replaces \n with <br /> in the variable $text.

Now, in includes/bbcode.php in phpBB2, you may see a line like this on line 159:

Code: Select all
str_replace("[quote:$uid]", $bbcode_tpl['quote_open'], $text);

Let's go through it showing what each particular thing means. First off, we have str_replace. This function is very easy to use and to understand. Basically, the format for a str_replace function could be as follows:

Code: Select all
str_replace($one, $two, $text);

The first variable is what string we will be replacing. And the second variable is what we will be replacing the first with. And $text is the variable we will be searching for the determined replacements.

So back to our snippet from phpBB2:

Code: Select all
str_replace("[quote:$uid]", $bbcode_tpl['quote_open'], $text);

The first part we have is "[quote:$uid]". Obviously we know that [quote is a part of a supported BBcode. The $uid variable is basically a unique id for the message, and finally we end it with the bracket ]. What we just covered was the string that str_replace will be searching for. Next, we have $bbcode_tpl['quote_open']. This is the variable that eventually leads to a .tpl file to show what str_replace should replace the BBcode with. If you open templates/subsilver/bbcode.tpl, you will find BEGIN quote_open. This is what the quote BBcode is replaced with:

Code: Select all
<table width="90%" cellspacing="1" cellpadding="3" border="0" align="center">
<tr>
     <td><span class="genmed"><b>{L_QUOTE}:</b></span></td>
   </tr>
   <tr>
     <td class="quote">

Think you're catching on? Good, we're almost done. Finally, we have the last part of the function, $text. $text is the post a user makes. So in conclusion, str_replace, and also ereg_replace and preg_replace, determine what to look for, determine what to replace it with, and determine where to look for it.

Hope this tutorial was helpful for you! Happy coding.

// Phantom
Last edited by Phantom on Sun Jun 05, 2005 2:29 pm, edited 1 time in total.
Phantom
Registered User
Registered User
 
Posts: 245
Joined: Mon Apr 18, 2005 3:24 am

Postby Thatbitextra on Sun Jun 05, 2005 2:05 pm

I don't suppose you could make this a full blown string replacement tutorial could you? I don't think I'm the only person here who would appreciate it :)
The more relation to bbcode.php the better ;)
Thanks for what you've written so far, though :)
My MODs: [NEW] Choose Who to Accept PMs From (Prevents you from receiving unwanted PMs!) | Warn of Old Topic Before Posting Reply
Style: subBlack (Now updated to phpBB 2.0.22 and 5 new color schemes!)

Please note, I do NOT offer support via PM. It's a forum, so post your question, then everyone can help.
User avatar
Thatbitextra
Moderator
Moderator
 
Posts: 1821
Joined: Thu Mar 24, 2005 1:11 pm
Location: A place where something is or could be located; a site.

Postby Phantom on Sun Jun 05, 2005 2:30 pm

Alright, I edited my post. Hopefully that is what you were looking for.
"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 Thatbitextra on Sun Jun 05, 2005 2:39 pm

Hmm. If you could explain preg_replace I'd be very grateful :)
And the use of things like \\2 in bbcode.php. And the need for first and second passes... Maybe you could go through [ color ] step by step? I'm not trying to be awkward, I seriously don't get preg_replace and bbcode.php generally confuses the hell out of me :(
Thanks :)
My MODs: [NEW] Choose Who to Accept PMs From (Prevents you from receiving unwanted PMs!) | Warn of Old Topic Before Posting Reply
Style: subBlack (Now updated to phpBB 2.0.22 and 5 new color schemes!)

Please note, I do NOT offer support via PM. It's a forum, so post your question, then everyone can help.
User avatar
Thatbitextra
Moderator
Moderator
 
Posts: 1821
Joined: Thu Mar 24, 2005 1:11 pm
Location: A place where something is or could be located; a site.

Postby Phantom on Sun Jun 05, 2005 3:51 pm

Will do. In fact, I'll make an entirely new one. Because what confuses you takes an extremely long time to explain.
"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


Return to PHP Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests

cron