Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stripos returns false when special characters is used

I am using the stripos function to check if a string is located inside another string, ignoring any cases.

Here is the problem:

stripos("ø", "Ø")

returns false. While

stripos("Ø", "Ø")

returns true.

As you might see, it looks like the function does NOT do a case-insensitive search in this case.

The function has the same problems with characters like Ææ and Åå. These are Danish characters.

like image 268
foens Avatar asked Mar 30 '11 14:03

foens


People also ask

Is Strpos case sensitive?

strpos() Function: This function helps us to find the position of the first occurrence of a string in another string. This returns an integer value of the position of the first occurrence of the string. This function is case-sensitive, which means that it treats upper-case and lower-case characters differently.

What is Stripos PHP?

The stripos() function finds the position of the first occurrence of a string inside another string. Note: The stripos() function is case-insensitive. Note: This function is binary-safe.

Is str contains case sensitive PHP?

Note that strpos() is case sensitive, if you want a case-insensitive search, use stripos() instead. Also note the === , forcing a strict equality test. strpos CAN return a valid 0 if the 'needle' string is at the start of the 'haystack'.


2 Answers

Use mb_stripos() instead. It's character set aware and will handle multi-byte character sets. stripos() is a holdover from the good old days when there was only ASCII and all chars were only 1 byte.

like image 54
Marc B Avatar answered Nov 04 '22 19:11

Marc B


You need mb_stripos.

like image 39
awm Avatar answered Nov 04 '22 19:11

awm