Re: RegExp Problem

From: Date: Wed, 18 Oct 2000 08:25:05 +0000
Subject: Re: RegExp Problem
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-20841@lists.php.net to get a copy of this message
Mukul Sabharwal writes: > Hi, > > I want to do the following : > > a variable with the following values to be accepted > ... > > if $variable contains > > (1) numbers > (2) underscores > (3) letters > > to be approved and anything else to be disapproved.. > how do i do this.... > > I would like to use pcre for this (preferably) but > ereg would also do.... > > thanks... With preg_match(): if (preg_match('/^\w+$/', $str)) { echo "Matched\n"; } else { echo "Didn't match\n"; } With ereg(): if (ereg('^[[:alnum:]_]+$', $str)) { echo "Matched\n"; } else { echo "Didn't match\n"; } Both of these require the string to not be empty: if the string may be empty, replace the '+' with a '*'. Good luck, Torben -- +----------------------------------------------------------------+ |Torben Wilson <torben@php.net> Netmill iTech| |http://www.coastnet.com/~torben http://www.netmill.fi| |Ph: 1 250 383-9735 torben@netmill.fi| +----------------------------------------------------------------+

« previous php.general (#20841) next »