filmov
tv
How to find a string inside another string using PHP

Показать описание
Finding a string is simple but there are some tricky points that you need to know.
- we can use strpos() function to find the position of a string inside another string.
- but strpos is case sensitive. In case you want case-insensitive comparison then use stripos() instead.
- NOTE: always be careful while using strpos or stripos functions. be if a sub string exists at zero index of main string, then these functions will return zero which is interpreted as FALSE in PHP. so in such case even-though search string matches but your script may wrongly say NOT FOUND. to avoid this please use type comparison like $pos !== FALSE
Here is the real PHP script:
$name = "Malik Ahmed Khan Awan";
$pos = stripos($name, "ahmed");
if($pos !== FALSE) {
echo "FOUND";
} else {
echo "NOT FOUND";
}
- we can use strpos() function to find the position of a string inside another string.
- but strpos is case sensitive. In case you want case-insensitive comparison then use stripos() instead.
- NOTE: always be careful while using strpos or stripos functions. be if a sub string exists at zero index of main string, then these functions will return zero which is interpreted as FALSE in PHP. so in such case even-though search string matches but your script may wrongly say NOT FOUND. to avoid this please use type comparison like $pos !== FALSE
Here is the real PHP script:
$name = "Malik Ahmed Khan Awan";
$pos = stripos($name, "ahmed");
if($pos !== FALSE) {
echo "FOUND";
} else {
echo "NOT FOUND";
}