Re: Strings and regular expression?

From: Date: Fri, 07 Feb 2003 22:01:21 +0000
Subject: Re: Strings and regular expression?
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-134810@lists.php.net to get a copy of this message
> > > $a1="1,8,98,244,9,243,2,6,36,3,111,"; > > > > > > $a2="8,98,244,9,243,2,1,6,36,3,111,"; > > > > > > How can I exactly match "1," in both strings using one regular expression? > > > (first "1" is the first element and second time in a random position)? > > > > if(preg_match('/(^|,)1(,|$)/',$str)) > > { echo "one is found"; } > > if(strstr($string, '1,') == 'FALSE){ > echo 'No match found'; > }else{ > echo 'Found a match'; > } > > No need for costly regex Very true, I agree. For your solution, though, wouldn't it correctly match a string such as '2,3,11,12' even though there is no entry that's just one? I assumed that one could be the last value in the string also, though, and hence the regular expression. If the 1 can be the last number in the string, then you could also use something like your solution (looking for ',1,') and include an OR that checks for the last two characters being ',1' OR the first two characters being '1,' Check if that or the regex is faster. ---John Holmes...

« previous php.general (#134810) next »