RE: [PHP] Strings and regular expression?
> > 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? > > Ok, why not something like this.. > There is almost always a better solution than regex > > > $string = '98,244,9,243,2,6,36,3,111,'; > > if(catchOne($string) == 'TRUE'){ > echo 'Found a One'; > }else{ > echo 'No match found'; > } > > function catchOne($string){ > > $arr = explode(',', $string); > > // check if the 1 is in the array > if(in_array('1', $arr)){ > return TRUE; > }else{ > return FALSE; > } > } I love it when questions like this come up. Honestly, you can use any solution you want and it's not going to make that much of a difference. But... it's always fun to benchmark them all and see which one wins. Here's the code I tried (which uses PEAR Benchmark) $str = '1,2,3,4,5,6,7,8,9,0'; $timer->start(); if(preg_match('/(^|,)1(,|$)/',$str)) {} $timer->setMarker('Preg'); if(in_array('1',explode(',',$str))) {} $timer->setMarker('Explode'); if(strstr($str,',1,') || substr($str,0,2)=='1,' || substr($str,-2)==',1') {} $timer->setMarker('Strstr'); $timer->stop(); $timer->display(); and, the results... Method Elapsed Time Preg0.80 Explode 0.65 Strstr 0.65 Make your own conclusions... Since the last solution actually uses three tests, it is even faster overall when the first condition comes out true and then gets slower if it has to check the second and then third condition. So, putting whatever condition is true the majority of the time will make that solution more efficient. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strings and regular expression?
This one time, at band camp, "1LT John W. Holmes" <[EMAIL PROTECTED]> wrote: > > 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? Ok, why not something like this.. There is almost always a better solution than regex Kevin -- __ (_ \ _) ) | / / _ ) / _ | / ___) / _ ) | | ( (/ / ( ( | |( (___ ( (/ / |_| \) \_||_| \) \) Kevin Waterson Port Macquarie, Australia -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strings and regular expression?
> > > $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... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strings and regular expression?
This one time, at band camp, "1LT John W. Holmes" <[EMAIL PROTECTED]> wrote: > > $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 Kevin -- __ (_ \ _) ) | / / _ ) / _ | / ___) / _ ) | | ( (/ / ( ( | |( (___ ( (/ / |_| \) \_||_| \) \) Kevin Waterson Port Macquarie, Australia -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Strings and regular expression?
> $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"; } ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Strings and regular expression?
Hi guys, I have 2 strings $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)? Thanks, Radu -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php