Re: Re: Quickly verifying single word.
| From: | Usamah M. Ali | Date: | Thu, 05 Jun 2008 09:20:30 +0000 |
| Subject: | Re: Re: Quickly verifying single word. | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-275048@lists.php.net to get a copy of this message | ||
On Thu, Jun 5, 2008 at 12:02 PM, Usamah M. Ali <usamah1228@gmail.com> wrote:
> Won't work either. The problem lies in using the === comparison
> operator. preg_match() returns 0 if no match is found, and FALSE if an
> error occurred. So using === will always echo 'No spaces' whether
> there were spaces or not, provided that no error has occurred.
>
> if (!preg_match('/\s+/i', $string))
> {
> echo 'No spaces';
> }
>
> should suffice.
>
> Regards,
> Usamah
>
Won't work either. :)
This should work fine:
if (preg_match('|\s+|i', $string) === 0)
{
echo '$string does not contain white spaces!';
}
elseif preg_match('/\s+/i', $string)
{
echo 'One or more white spaces found!';
}
else
{
echo 'An error has occurred!';
}
According to your needs, it could be simply shortened to the first if:
if (preg_match('/\s+/i', $string) === 0)
{
echo 'No white spaces';
}
Hope that works well.
Regards,
Usamah