Contains instead of equal

I like to know whether the value of a variable has a special designated word in it.

The code below doesn’t work correctly, but I hope it shows what I want.

<?php
$myVar="this is a book";

if ($myVar [COLOR="Red"]contains[/COLOR] "is")
{echo Yes, it's 3rd person singular;}
else
{echo No, it's NOT 3rd person singular;}
?>

strpos() will probably help


if(strpos($myVar,'is') !== false) {
     echo "Yes, it's 3rd person singular";
} else {
     echo "No, it's NOT 3rd person singular";
}

strpos()

What is the full name of “strpos”?

String Position or something?

Won’t that show true though for both of these sentences?

“this is a book”

“this a book”

are you sure you shouldn’t be asking oddz that question?

if you run the code he/she posted then the answer is yes.

but I wouldn’t pass the paramater to strpos() in the same way as oddz did if you’re looking for words.

another option could be to use explode()

Yes, You’re right. I’ll consider it.:blush:

His response didn’t exist when I opened and left the tab open a while before replying, so guess who’s the lucky receiver. You! :slight_smile:

You wouldn’t? What would you do instead?

I’m surprised you saw mine and not oddz’s because the timestamp for both posts on my pc is 08:52 (my local time).

I guess the following code produces the same result.
and 1 letter shorter.

if(strpos($myVar,'is') == [COLOR="Red"]true[/COLOR]) {
     echo "Yes, it's 3rd person singular";
} else {
     echo "No, it's NOT 3rd person singular";
}

Furethermore, thinking way is a little more simple.

When I read “!== false”, I have to read it 2 times for understanding it,
When I read “== true”, I can understand it by 1 time of reading it.

Thank your, oddz.

By the way, what is the full name of “strpos”?

String Position or something?

So am I, but minutes have seconds, which also contain milliseconds.
When I loaded the page your reply was the most recent post on my page. Get over it.

ok no problem :),

you can ask your original question to oddz then since it’s his/her code and not mine if you feel it woudn’t do what the op wanted.

Perhaps an understanding of strpos will help.

When it fails to find the string, it returns false.
When it succeeds, it returns the position in the string, which could anywhere from 0 on upwards.

If it’s at 1 or greater, that will be equivalent to a truthy value, but if it’s at 0 then it won’t.

So you need to check that it’s !== false to catch the error. Checking for true just won’t work in this case.

What you could do is check that it’s === false, then use an else condition to handle to found situation.


if (strpos(...) === false) {
    // not found
} else {
    // is found
}

When you don’t want to handle the not found situation, you can invert the comparison:


if (strpos(...) !== false) {
    // is found
}

I wasn’t worried about the code as it didn’t appear to exist when I made my comment. What I was worried about was your “idea” of using strpos, because you were saying that it would probably help. It won’t though, as it would find the position within other words as well.

What is needed instead is a way to find the position of the whole word “is” so that other words like “this” won’t be considered a valid match.

As a followup, what could be used instead is a regular expression match, where \b is used to match word boundaries.

Something like this should do the trick:


if(preg_match("/\\bis\\b/", $myVar) > 0) {

So I assume you are also saying that the code posted by oddz would not help as well.

but I’m not convinced that strpos() could not do the job if used properly in conjuction with other functions as well.

that’s why I said it could “help”.

in the past I have also used explode() in conjuction with other functions to find words in strings as in my BadWordsDetector class I posted yesterday.

I am convinced there is more than 1 way to do what the op wanted.

Yes of course.

You could add more complexity to get the job done, but who wants that?
Keep it simple and easy to understand, so that when you, or a homicidal maniac look over the code some 6 months later, the intention of the code is just as easy to understand.

There is always more than 1 way.

By the way, here’s more info on word boundaries

yep no problem :slight_smile:

but simple is a relative term and what might be simple for one person might be difficult for another and vikky-verky.

How about this?

When using preg_match, where no match is found it returns 0. Where 1 or more matches are found it returns 1.

Using \b for the word boundary, we can search for \bis\b to successfully find it in “This is a book” and not find it in “This a book”

That’s pretty simple.