0 0 Votes

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

Question by Guest | 10/02/2014 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

Sledge

Show Profile | Message
Avatar
0Best Answer
0 Votes

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. 
10/02/2014 at 20:24

ReplyPositive Negative
Reply

Related Topics

PHP: Cut off String at last Space
Open Question | 1 Answer

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 articles on askingbox.com. How to do.