How to make stripos() work with multibyte strings?
Asked Answered
S

4

7

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.

Sizzler answered 30/3, 2011 at 14:46 Comment(0)
Y
9

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.

Yaker answered 30/3, 2011 at 14:51 Comment(2)
Why oh why is this function not stated as a related function to stripos... Then I would have been able to find out myself. Thanks for the help.Sizzler
You can suggest it to the PHP documentation maintainers. Those doc pages aren't frozen for all eternity.Yaker
I
3

You need mb_stripos.

Inextricable answered 30/3, 2011 at 14:51 Comment(1)
As much as I would like to accept both yours and Marc B's answer, I selected his since an explanation accompanied his answer.Sizzler
B
1

mb_stripos will take care of this.

Blesbok answered 30/3, 2011 at 14:54 Comment(0)
B
1

As the other solutions say, try first with mb_stripos(). But if using this function doesn't help, check the encoding of your php file. Convert it to UTF-8 and save it. That did the trick for me after hours of research.

Blubberhead answered 15/6, 2015 at 2:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.