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
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