Simple PHP Strpos Function Not Working, Why

Simple PHP strpos function not working, why?

When in doubt, read the docs:

[strpos] Returns the numeric position of the first occurrence of needle in the haystack string.

So you want to try something more like:

// ...
if (strpos($link, $unacceptable) !== false) {

Because otherwise strpos is returning a number, and you're looking for a boolean true.

strpos function not working with special character

is a multi byte character and strpos cannot understand this.

What you are looking for is mb_strpos. This function performs the same action, but is multi-byte characters safe.


Update: While the answer above is correct, it was not the entire solution to your case. The problem is you are making the if check too complicated.

You are first inverting the result of strpos and then checking if it does not match false. I don't think this was your intention.
To fix this, just check if the result of mb_strpos equals false.

if (mb_strpos($name_to_check, '★ StatTrak™') === false)
echo 'not contain';
} else {
echo 'contain';
}

Why is strpos working the first time, but not the second?

As stated in the official documentation:

Warning

This function may return Boolean FALSE, but may also return a
non-Boolean value which evaluates to FALSE. Please read the section on
Booleans for more information. Use the === operator for testing the
return value of this function.

You are checking the result with == true instead of !== false.

So, try this code:

if (strpos($word, "mono") !== false) {
$type = "Monobloc";
} else {
$type = "Articulated";
}

if (strpos($word, "galva") !== false) {
$coating = "Galvanized Rod";
} elseif (strpos($word, "epoxi") !== false) {
$coating = "EPOXI 100%";
} elseif (strpos($word, "electro") !== false) {
$coating = "Electrozinced";
}

strpos not working for a certain string

strpos returns the index of the occurrence in the string (or false if it isn't found). When this index is 0, the condition: (strpos("show me how to dance", "show me")) is evaluated as false (because in PHP: 0 == false is true). To be sure the needle is found (even at index 0) you need to use a strict comparison:

if (strpos("show me how to dance", "show me") !== false)

simple strpos() not working with [ char, why?

It looks like you swapped the arguments:

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )

So if you want to know if a [ is in your string [rgeger]:

if (strpos("[rgeger]", "[") !== false) { 
echo 'hey';
}

(Source: http://php.net/strpos)

PHP 7.4.3 Strpos is not working on server but locally

Did you try strpos($result, '1') !== false?

If condition not working with strpos

From the manual entry of strpos() function:

Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

Returns FALSE if the needle was not found.

which means that if($Postion !== TRUE) will always be true, as strpos() never returns true.

To make your function work as expected, change your if statement to if($Postion === false).



Related Topics



Leave a reply



Submit