|
|
|
NewbieNotes is a little series of tips for people who are new to PHP to give them a few handy tips that the more experienced of us use often
No matter what you want to do with PHP one day you're going to need to use regex (regular expressions)
There's lots and lots available to read but how come when you put it in your first regex it don't work?
The most obvious thing is that you didn't escape one of the proliferation of back-slashes in a typical regex srting
Usage Example
| | looking for a whitespace characher in regex is denoted by \w - your PHP needs to write that as \\w - i.e. every \ in the real regex string needs to be doubled up | |
OK - it still don't work - experiment without PHP in the way!
Pop off to http://www.weitz.de/regex-coach/ and download Regex Coach. This excellent little program will show you exacly what your regex string is doing
Now, as long as you remember to escape your string properly you can experiment to you're heart's content and learn by experimentation
You'll still need the regex reference, this will just make it more understandable |
|
| a PHP Function to Get only the filename (remove the extension) using regular expressions. Categories : PHP, Regexps, Beginner Guides | | | Validating a URL with preg_match Categories : PHP, Regexps, Beginner Guides, Data Validation | | | Username Validation function Categories : PHP, Beginner Guides, Regexps | | | Cut your MySQL Connections to 1 line of code Categories : PHP, Beginner Guides, Databases, MySQL | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | Basic Authentication with sessions Categories : PHP, Beginner Guides, Authentication, Form Processing, Sessions | | | Introduction to Language Files Categories : PHP, Filesystem, Beginner Guides | | | News management class Categories : PHP, PHP Classes, Beginner Guides | | | A flat file counter Categories : PHP, Cookies, Filesystem, Beginner Guides | | | A very simple PHP single password cookie based login without usernames. Categories : PHP, Cookies, Security, Beginner Guides | | | PHP Email image generator - hide your email from bots - using the GD Library Categories : PHP, Graphics, GD image library, Beginner Guides | | | Simple way to replace a variable value in a .conf (.ini) file using a
webbrowser - the first stage of a complete universal configuration editor Categories : PHP, Regexps, Code Editors, Filesystem | | | ereg -- Regular expression match Categories : PHP, PHP Functions, Regexps | | | Different Call User Functions Categories : PHP, Functions, Beginner Guides | | | Creating a Language File Categories : PHP, Beginner Guides, Filesystem | |
| | | | Shiraz Esat wrote : 1086
The most obvious thing is that you didn`t escape one of the proliferation of back-slashes in a typical regex srting
e.g. looking for a whitespace characher in regex is denoted by \w - your PHP needs to write that as \\w - i.e. every \ in the real regex string needs to be doubled up
Regarding having to double up \s: this only applies if you`re double quoting (") as opposed to single quoting (`) (when single quoting, though, you`ll need \` if you wish a ` to be a part of your regex).
It`s important to know how PHP treats strings, and the different between double-quoting, single-quoting and heredocs.
| | | | Jose Santos wrote :1104
Thanks!
The Regex Coach it`s very good !!
The regular expressions in php is very useful.
See http://www.phpbuilder.com/manual/pcre.pattern.syntax.php.
It`s a very good website to describe regular expressions
| |
|
|
|