I have to check if an asterisk exists in the string, but it always shows that there is no asterisk.
Why does strpos()
not work? I have also tried stripos()
, mb_strpos()
, and mb_stripos()
without luck.
<?php
$str = "****123";
if(strpos($str, '*') == false){
echo 'There is no asterisk in the string';
} else {
echo 'There is asterisk in the string';
}
There is no asterisk in the string.
strpos()
returns an integer (0 or more) or FALSE
if not found. In PHP, the comparison with 0
and FALSE
are different only if you use a strict equality (===
):
$str = "****123";
if(strpos($str, '*') === false){
echo 'There is no asterisk in the string';
} else {
echo 'There is asterisk in the string';
}
Outputs:
There is asterisk in the string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With