Re: problem with if and exact match
| From: | Simon J Welsh | Date: | Tue, 15 Mar 2011 05:09:29 +0000 |
| Subject: | Re: problem with if and exact match | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-311853@lists.php.net to get a copy of this message | ||
On 15/03/2011, at 6:02 PM, Jack wrote:
> I want to be able to match if a string is contained within the string I am
> evaluating.
>
>
>
> I know that if ( $name == "xxjacksonxx"); based on the below would be true.
>
> But I want to be able to say if "jackson" is contained within $name that
> it's a match.
>
>
>
> I tried the below without success..
>
> Not getting the operand properly..
>
>
>
> <?
>
>
>
> $name = "xxjacksonxx";
>
>
>
> if ( preg_match($name, "jackson")) {
>
> print "true";
>
>
>
> } else {
>
>
>
> print "false";
>
> }
>
>
>
> ?>
>
>
>
> Thanks!
>
> Jack
if(strpos($name, 'jackson') !== false) {
true
}
You can use stripos for case-insentive matching.
Using regex functions when you don't need the power is overkill and slower. Your call to
preg_match wasn't working because you need your search term first, and it needs proper
delimiters.
---
Simon Welsh
Admin of http://simon.geek.nz/
Who said Microsoft never created a bug-free program? The blue screen never, ever crashes!
http://www.thinkgeek.com/brain/gimme.cgi?wid=81d520e5e