开发者

How to find apostrophe in string using PHP?

I can't get to detect an apostrophe (') in a string. I tried

if (strpos($username, chr(39)) > 0 )
if (strpos($username, '\') > 0 )
if (strpos($username, "'") !== FALSE)) 

without luck. What开发者_运维问答's the right way of doing it?


you listed this one, and it should work:

if (strpos($username, "'") !== FALSE)


Single-quote is a special character. So if you want to use a single-quote within the single quoted string you have to escape the single-quote with a backslash \ symbol.

int singleQuotePosition = strpos($username, '\'');

OR

int singleQuotePosition = strpos($username, "'");

PHP Manual: Strings


Just another random guess: Maybe your single quote isn't really a single quote.

If so, you might want to try mb_strpos or preg_match to find the UTF-8 variations of that character:

preg_match("/'/u", $string);

Or even test with /\p{Pi}/u to see if it's another type of single quote doppelganger.


Another tip: instead of strpos and boolean result fiddling, try strstr if you just want to test for a character presence.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜