Re: regular expression

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2 June 2010 06:12, Peter <peters@xxxxxxxxxxxx> 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux