11 Vote

PHP: Does a string contain a word independent from upper or lower case writing?

Question by Guest | 2014-02-10 at 15:37

I would like to determine using an if-statement whether specific string is containing a specific word (thus another string). With this, the difficulty is that I do not know, whether the word is written in uppercase or lowercase letter. So, I need something like a case insensitive search.

Up to now, I am using the function strpos($str, $searchstr).

However, the problem using strpos is, that this function does not differ writings, so "ABC" and "abc" are treated as different strings, but they should be treated as equal.

Does someone have an idea what I can do?

ReplyPositiveNegative
1Best Answer1 Vote

Spontaneously, I have two solutions in mind.

On the one hand, you can use strtolower or strtoupper to change the whole string to the same writing before the comparison completely:

$pos = stripos(strtolower($s), strtolower($searchstr));

Or you can just use stripos:

stripos($s, $searchstr);

The PHP-function stripos finds a string in another string independent from their writing (case insensitive).

Alternatively, of course, you can also use regular expressions. If you want, a search for the PHP-function preg_match will help you. But I think, stripos is sufficient in your case. 
2014-02-10 at 20:24

ReplyPositive Negative
Reply

Related Topics

PHP: Current Date and Time

Tutorial | 0 Comments

HTACCESS: Simplify URL

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.