TalkPHP
 
 
Account Login
Latest Articles
» How to keep your forms from double posting data
» cURL Basics
» Securing your PHP applications Part 1
» The way the function rolls
» Database Abstraction with Zend_Db - Part 2
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack (1) Thread Tools Display Modes
Old 04-25-2008, 07:03 AM   #41 (permalink)
The Visitor
 
Join Date: Apr 2008
Posts: 1
Thanks: 0
kikloo is on a distinguished road
Default

Hi,

PHP Code:
// Check Valid E-Mail
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"$_POST["email"])) { echo "Email Address is not valid."; }

// Check valid URL
if (!preg_match('/^(http|https):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\//i'$_POST["url"])) { echo "Website URL is not valid."; } 
Thanks.
kikloo is offline  
Reply With Quote
Old 05-29-2008, 12:55 AM   #42 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

Multiline regular expressions... is it best just to use the s modifier? For example, if I wanted to remove all PHP style comments from a file (no, I'm not trying to make a compression function like Kalle's, just playing around with different methods of templating), I'm currently using;

~/\*.*?\*/~s

What about without the s? I got this to work:

~/\*(.*?\r?\n?)+?\*/~

Can you think of a more efficient way?
-m

Last edited by delayedinsanity : 05-31-2008 at 02:32 PM.
delayedinsanity is offline  
Reply With Quote
Old 05-31-2008, 11:24 AM   #43 (permalink)
The Contributor
RegEx Guru 
 
Geert's Avatar
 
Join Date: Dec 2007
Location: Belgium
Posts: 58
Thanks: 5
Geert is on a distinguished road
Default

Your second regex is a nice experiment, however, my guess is that it is horribly inefficient because of the combination of (lazy) quantifiers.

Note that there is nothing wrong at all with using the s modifier.

In Friedl's book you'll read about a more efficient way. I believe it also takes into account situations like /* echo '*/'; */.
Geert is offline  
Reply With Quote
Old 05-31-2008, 02:31 PM   #44 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

I reduced the lazyness a bit by changing it to this: ~/\*(.|[\r\n])*?\*/~ so now we're down to just one. I don't know much about lookahead and look behind assertations which may be incorporated in the friedl example if it's anything like his example for matching html style tags. Maybe you could post it here so we could dissect it?
-m
delayedinsanity is offline  
Reply With Quote
Old 05-31-2008, 03:14 PM   #45 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

How about this? It doesn't count for nested comments, which means it'll still break using your haystack Geert, but it doesn't use any (I repeat any!) lazy operators. Based off an example I posted elsewhere yesterday, I've updated it - so we're looking for an opening and closing tag, but the in between is a little smarter than before where it just looked for anything and everything. It looks for any character except for another star, or a star but only if it's not the closing tag, or a new line.

PHP Code:
$szTest = <<<EOF
/*** this
is*** the
test **/
/* test*/
booga!

/* more
new lines
in this
comment
*/
EOF;

$szTest preg_replace("~/\*([^*]|\*+[^*/]|[\r\n])*\*+/~"'booga!'$szTest);
//$szTest = preg_replace("~/\*(.|[\r\n])*?\*/~", 'booga!', $szTest);
echo $szTest
  1. /\*([^*]|\*+[^*/]|[\r\n])*\*+/
  2. /\*(.|[\r\n])*?\*/
...but which one is more efficient? The second one has a lazy operator, but it matches the same as the first one. I like the second one because it's more readable, but I'm prone to using things like the first one because it's less readable and people looking at my work may think I'm actually smarter than I really am.

Other than the ego, I also like the first one because it's job description is much more defined. Despite accomplishing the same task, it's doing what it's meant to do, whereas the second, although accomplishing the same task for now, has more ability to let something slide through that it shouldn't in the future. Or will it... hmmmmmmmmmmmmm.
-m

edit: Here's a hackish version that matches newlines without mentioning newlines: /\*[\w\W]*?\*/

Last edited by delayedinsanity : 05-31-2008 at 03:55 PM.
delayedinsanity is offline  
Reply With Quote
Old 07-31-2008, 09:47 AM   #46 (permalink)
The Visitor
 
Join Date: Jul 2008
Posts: 1
Thanks: 0
xcasio is on a distinguished road
Default

Hey,

Just a small tip. A perfectly valid e-mail address (example@example.museum) would not validate in this way due to the 4 character limit on the top level domain.
xcasio is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/advanced-php-programming/1612-8-practical-php-regular-expressions.html
Posted By For Type Date
Digg - Regular expressions for the PHP NooB! This thread Refback 12-25-2007 05:06 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:18 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0