php preg_match string pattern help.

Alphax alphasigmax at gmail.com
Wed Nov 15 05:41:18 CST 2006


Jim O'Halloran wrote:
> 
> 
> Tim C Bruhn wrote:
>> Hi, I am trying to write a little bit of PHP and i've run into some
>> problems.
>>
>> $pattern = ?????noidea?????
>> foreach($array as $row)
>>      {
>>           if (preg_match($pattern, $row))
>>           {
>>          echo "A match was found.".$row;
>>           } else {
>>          echo "A match was not found.";
>>           }
>>   }
>>
>> the only problem is that i have no idea what the string pattern has to
>> look like to search/match a single dollar sign in the middle of a
>> string. Any idea would be greatly appreciated.
>>
> preg_match is probably overkill in this case because you don't need to
> match on a regular expression, just a simple character (or sequence of
> characters).  strstr (http://au.php.net/strstr) is probably more
> appropriate (and simpler) in this case.  Usng strstr your if statement
> would look like this:
> 
> if (strstr($row, '$') === false)
> 

You could probably do better than that even - this is on the strstr man
page:

> Note: If you only want to determine if a particular needle occurs
> within haystack, use the faster and less memory intensive function
> strpos() instead. 

if (strpos($row, '$') === false)

http://au.php.net/strpos

HTH,
-- 
                Alphax
        Death to all fanatics!
  Down with categorical imperative!
OpenPGP key: http://tinyurl.com/lvq4g

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 569 bytes
Desc: OpenPGP digital signature
Url : http://www.linuxsa.org.au/pipermail/linuxsa/attachments/20061115/53bc9fed/signature.bin


More information about the linuxsa mailing list