PHP Coding Tips

Wednesday, August 8, 2012

How to check that one string contains another in PHP

If you want to check if one string contains desired string, you can use strpos function. But be aware - the problem rises when a needle is on position 0 of checked string. In that situation php strpos() returns 0, and it will return false if string is not found. So, to compare if a string is containing user string, use !== operatorif ( strpos( $string , 'somestring' ) !== false ) { }Also use === false if you want to check that string doesn't not contains data

No comments:

Post a Comment