TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack (1) Thread Tools Search this Thread Display Modes
Old 05-29-2008, 12:55 AM   #41 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
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   #42 (permalink)
The Contributor
RegEx Guru 
 
Join Date: Dec 2007
Location: Belgium
Posts: 60
Thanks: 6
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 '*/'; */.
__________________
Kohana - PHP5 framework
Geert is offline  
Reply With Quote
Old 05-31-2008, 02:31 PM   #43 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
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   #44 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
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   #45 (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
Old 04-22-2009, 11:22 AM   #46 (permalink)
The Visitor
 
Join Date: Apr 2009
Posts: 1
Thanks: 0
lazycoder is on a distinguished road
Default email validation is wrong

hey, email validation is still problematic, not valid
as it will accept _lazycoder@_.com as valid

Please try this one

PHP Code:
$pattern "/^[a-z]+[-|_|.]?[a-z0-9]+@[a-z0-9]+[-|.|_]?[a-z]+\.[a-z]{2,4}$/"
Hope it helps someone
lazycoder is offline  
Reply With Quote
Old 10-19-2009, 09:25 PM   #47 (permalink)
The Wanderer
 
bucabay's Avatar
 
Join Date: Oct 2009
Location: Fiji
Posts: 6
Thanks: 0
bucabay is on a distinguished road
Default

For email address validation there is two libraries on Google Code.

1) validating email syntax with regex
http://code.google.com/p/php-email-address-validation/

2) validating email's via SMTP
http://code.google.com/p/php-smtp-email-validation/
bucabay is offline  
Reply With Quote
Old 11-01-2009, 01:46 PM   #48 (permalink)
The Wanderer
 
nuweb's Avatar
 
Join Date: Nov 2008
Location: Yorkshire, England
Posts: 8
Thanks: 1
nuweb is on a distinguished road
Default

The Opening Post, is regarding regular expressions for many aspects where its not needed?

As filter_var works very well for me.

Validate Number
PHP Code:
$data filter_var($dataFILTER_SANITIZE_NUMBER_INT); 
Validate Email
PHP Code:
$data filter_var($dataFILTER_VALIDATE_EMAIL); 
Validate Url
PHP Code:
$data filter_var($dataFILTER_VALIDATE_URL); 
Validate Boolean
PHP Code:
$data filter_var($dataFILTER_VALIDATE_BOOLEAN); 
Validate String
PHP Code:
$data filter_var($dataFILTER_SANITIZE_STRING); 
Validate IP
PHP Code:
$data filter_var($dataFILTER_VALIDATE_IP); 
__________________
NuWeb
nuweb is offline  
Reply With Quote
Old 11-01-2009, 05:46 PM   #49 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

"The Opening Post, is regarding regular expressions for many aspects where its not needed?"

If you have a drivers license, that's great up until your car breaks down. At that point you're left on the side of the road, maybe you have a toolkit, but what are you going to do with it when you don't know how to work a ratchet?

Not only is it beneficial to understand how these functions are working underneath the surface (afaik, filter_var is just a wrapper for nearly the same regular expressions), but it's good practice for building regular expressions you may need down the road. Same thing if you need a variation on a theme; what happens if you're looking for a specific type of email address?

Good information to add though, filter_var() can be a handy tool to know in addition to regular expressions!
delayedinsanity is offline  
Reply With Quote
Old 10-16-2012, 09:13 AM   #50 (permalink)
The Wanderer
 
Join Date: Oct 2012
Posts: 10
Thanks: 0
lennondevid is on a distinguished road
Default

Good post. I think regular expressions are a very useful tool for developers. They allow to find, identify or replace text, words or any kind of characters.




php web application development | PHP development | php mysql development | cakephp developers

Last edited by lennondevid : 10-23-2012 at 12:12 PM.
lennondevid is offline  
Reply With Quote
Old 10-18-2012, 02:07 PM   #51 (permalink)
The Addict
 
Join Date: Oct 2012
Posts: 244
Thanks: 0
dashixiong is on a distinguished road
Default

Some conservatives have Coach Factory Outlet pushed that critique further, saying that Mr. Obama’s policies are too costly, often assist the wrong people Louis Vuitton Belts and could have the paradoxical effect of driving up college costs. The dispute turns not just on different Coach Factory Outlet assessments of how policies play out, but on differing philosophical views about the role of government. During Gucci Belts his time in office, Mr. Obama has sharply increased aid to low- and middle-income students, notably through the Pell Grant Coach Factory Outlet program, which grew from $14.6 billion given to 6 million students in 2008, to nearly $40 billion for Coach Factory Outlet almost 10 million students this year. His administration also made it easier to request aid, shortening the Coach Factory Online complex federal application and allowing people to transfer their financial information electronically from the Internal Coach Outlet Online Revenue Service database. But while many education experts laud his efforts, analysts of varying political Coach Outlet Online stripes have also questioned how much impact some of the president’s policies will have, noting that the prices Coach Online Outlet charged by colleges, and student borrowing, continue to climb.But behind the headlines about soaring costs, the Coach Factory Outlet Online reality is more complex and wildly uneven, because a growing number of students receive Coach Outlet Online financial aid, and only relatively high-income families pay those fast-rising sticker prices. Adjusted for Coach Factory Online inflation, the College Board calculates, the average net price changed little over the last decade at private Coach Factory Outlet schools, and rose only modestly at public ones.Defending federal spending, Arne Duncan, the secretary of Hermes Belts education, said that for more than 30 years, college prices had risen even when federal aid had not, leading him to believe Coach Factory Online there was zero correlation.
dashixiong is offline  
Reply With Quote
Old 10-22-2012, 10:00 AM   #52 (permalink)
The Addict
 
Join Date: Oct 2012
Posts: 244
Thanks: 0
dashixiong is on a distinguished road
Default Coach Outlet

when I go home.Coach Outlet It’s a big part of what I find addictive about living and working inCoach Outlet Store Online this part of the world. You feel like you’re watching the future unfold.Coach Factory Online for the US Open with no success."I prefer to go day-by-day without setting objectives orCoach Factory Online time frames, work hard on my recovery and make sure I keep on getting better little by little."Nadal has been forced Coach Outletto watch from the sidelines as longtime rival Roger Federer returned Coach Purse Outlet Onlineto the top of the world rankings after winning Coach Factory Outlet Onlinea seventh Wimbledon singles title.He has also seen Britain's Andy Murray,Coach Bags Outlet Online who won the men's singles gold medal at the 2012 Olympic Games, move above him in the world rankings after securing hisCoach Handbags Outlet first grand slam title at last month's U.S. Open.Currently ranked fourth in the world, Nadal has qualified to play at November's season-ending ATP World Tour Finals in London, Coach Outlet Onlinebut his ability to take part is in doubt due to his injury problems, admitting that his knee is still "bothering him".
dashixiong is offline  
Reply With Quote
Old 01-21-2013, 08:05 AM   #53 (permalink)
The Wanderer
 
Jack Hard's Avatar
 
Join Date: Dec 2012
Posts: 11
Thanks: 0
Jack Hard is on a distinguished road
Default

Highest position. Thank you! To test this, checkdate not let me down. Just go at once and tell you if it is available. Just check the number of days in the month is correct, even taking into account leap years! You can not beat this role, in my opinion, and I'm sure it's just a regular expression can match.
Jack Hard is offline  
Reply With Quote
Old 01-21-2013, 01:30 PM   #54 (permalink)
The Visitor
 
Join Date: Jan 2013
Location: South Africa
Posts: 2
Thanks: 0
chieflujja is on a distinguished road
Default

Well this is a very good forum i wonder if the owner know about the way people are spamming it i can manage it please who can direct me to the owner?
chieflujja is offline  
Reply With Quote
Old 01-29-2013, 12:07 PM   #55 (permalink)
The Addict
 
Join Date: Oct 2012
Posts: 244
Thanks: 0
dashixiong is on a distinguished road
Default

Organizers said Coach Outlet Online was opportune because the battle’s 150-year anniversary is in December, and Fredericksburg Coach Factory Outlet has been preparing to mark the sesquicentennial. in the new agreement is that Coach Outlet Online revolutionary councils from 14 Syrian provinces now each have a representative, though not all live Coach Online Outlet in Syria. The hope is that will bind the coalition to those inside the country. Perhaps Coach Bags Outlet the most important body the new group is expected to form is a Revolutionary Military Council Coach Factory Online to oversee the splintered fighting organizations and to funnel both lethal and nonlethal Coach Factory Outlet military aid to the rebels. It should unite units of the Free Syrian Army, various militias Coach Outlet Store Online and brigades in each city and large groups of defectors. Before the ink was even dry on the Coach Outlet Store final draft, negotiators hoped that it would bring them the antiaircraft missiles they crave to Coach Factory Stores take on the Syrian Air Force. The United States and Britain have offered only Coach Handbags Outlet nonmilitary aid to the uprising. A similar attempt by the Syrian National Council to Coach Factory Store supervise the military never jelled. Organizers said funding was too haphazard. Eventually foreign Coach Factory Online governments like Qatar and Saudi Arabia, which are financing and arming the rebels, found Coach Factory Online their own favorite factions to deal with. Foreign leaders notably including Secretary of State Coach Outlet Hillary Rodham Clinton urged this unification largely so they could coordinate their Coach Factory Outlet efforts and aid through a group of technocrats. Once it receives international recognition, the Coach Outlet Store Online coalition is supposed to establish a temporary Coach Outlet Online military never jelled.
dashixiong 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 06:06 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 07:16 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design