Re: regular expression
| From: | Richard Quadling | Date: | Wed, 02 Jun 2010 08:22:13 +0000 |
| Subject: | Re: regular expression | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-305736@lists.php.net to get a copy of this message | ||
On 2 June 2010 06:12, Peter <peters@egrabber.com> wrote:
> Hi Tanel,
>
> 1. only letters
>
> $str = 'helloworld';
>
> if(preg_match("/^[a-zA-Z]*$/",$str))
> echo "only letters";
> else
> echo "failed";
>
> 2. only letters and spaces
>
> $str = 'hello world';
>
> if(preg_match("/^[a-zA-Z\s]*$/",$str))
> echo "only letters and spaces";
> else
> echo "failed";
>
>
> Regards
> Peter.M
Be careful with using *.
The issue of a zero length string is important.
* will allow a zero length string.
++ will force the regex to match something, so zero length strings are rejected.
/s will match space, formfeed, newline, carriage return, horizontal
tab, and vertical tab
So a string with newlines (for example a <textarea> with line breaks)
would match.
--
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling