![]() |
8 Practical PHP Regular Expressions
For all us security paranoids :-) Here are eight practical PHP regular expressions i found on the web which came very handy to me:
Quote:
Quote:
PHP Code:
Quote:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
Quote:
PHP Code:
Hope you find them useful too. Source/credits: Devolio.org |
One question I have is isn't {3,3} exactly the same as {3}?
|
Let me start with thanking you for supplying these regular expressions. I intended to comment on one or two things but got carried away so please accept my apologies for the length of this reply!
Some comments:
Is devolio.com your site? |
Quote:
{3} three preceding characters and {3,3} three to three preceding characters. Will update the code above. ^^ Salathe, devolio is not my site. Anyways, big thanks for that great feedback, i really appreciate you pointing that stuff as i have relayed on some of these for previous projects and although i never got an error i see all the points you mention and im starting to panic now!, will try to update them so we all can use them. thanks again |
Quote:
|
Revised Regular expressions:
TELEPHONE PHP Code:
PHP Code:
POSTAL CODE PHP Code:
PHP Code:
PHP Code:
PHP Code:
Thanks again! |
Much better :-) ! Does that mean we can now look down on the original website which published the regular expressions? Oh let's!
Honestly though, it's good to see progress like this. I posted a comment on the website referring the blog owner to this thread - whether or not he (or she) does or not is another question. I just know I personally would as I really appreciate it when people tell me I could do something better. |
Thanks for taking the time to look over my comments! I really hate long posts full of points like that -- it's all to easy to come across as condescending, something I don't want people to see in my posts.
So for the revised patterns, good job with them. There are still some points that I'd like to pick up on but, mercifully, far fewer than before. To make up for that, I'll go into mind-numbing detail! :-)
Please note that all of the above points are just examples of what one could do and should by no means be seen as drop-in solutions for particular needs. Think of them more as handy starting points to refine for your exact needs. Go play! *!* |
Hey everyone,
Though I haven't had a chance to look through all of your responses, I've skimmed over most of them, and appreciate the feedback and improvements. I know, they aren't perfect, but (at one point or another,) they got the job done for me. When I get a chance tomorrow, I'll read through all of this more thoroughly. |
Awesome thread :D
|
Big thanks Salathe, i appreciate the care of details you shared in your feedbacks, i dont have a big background on regular expressions so this things are really eye openers, love to see how they can be improved.
Quote:
Quote:
|
We're all here to learn, Devolio :-) To be fair, even if I had done those regular expressions, Salathe would have pulled them apart. He's just too damned good! Grr. Seriously though, I love threads like this - feels really, community like, everybody pulling together to find the best solutions.
|
Finally got a chance to go through all of these (holidays are crazy,) and I couldn't agree more with most of your points.
And I more than appreciate all of the feedback, knowledge is power. :) Would anyone who's contributed to the updates mind if I posted the updates on my site with credit to the site? (or to the individuals?) |
Thanks for taking the time to wade through the comments, devolio. I'm quite happy for anything that I've said to be posted up on your site as I'm sure everyone else here is (but best let them say that explicitly). We're all about sharing and building on each others' strength here. :-)
|
I don't mind you crediting TalkPHP at all. I can't speak for the individuals though :-) !
Thank you, Devolio! |
If you need one for an email address (here's something i came across in my functions file, not sure who's it is, but it's not mine)
PHP Code:
|
As long as the individual contributors don't mind.
Also, I've (heard) that validating e-mail addresses with Regex is almost impossible, and have been pointed to this a few times. http://www.iamcal.com/publish/articl...parsing_email/ |
One or two questions, WinSrev.
php Code:
Surely that variable will also be empty as it's not being set anywhere - unless I'm overlooking something. Also, you may wish to use preg_match as opposed to the ereg_*/eregi_* function set as the former is faster, and they're removing the latter from PHP 6. |
Im glad to see how much this thread is progressing. Devolio, i wouldnt mind either to see you credit TalkPHP ;-)
Here is another to validate URLS i made yesterday, it's not perfect at all so maybe we can improve it to make it better: /^(http|https|ftp):\/\/([\w]*)\.([\w]*)\.(com|net|org|biz|info|mobi|us|cc|bz|tv|ws|name |co|me)(\.[a-z]{1,3})?\z/iPHP Code:
|
Quote:
|
| All times are GMT. The time now is 03:26 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0