Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if asterisk exists in a string

Tags:

php

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';     
}

Desired output:

There is no asterisk in the string.

like image 477
Arslan Developer Avatar asked Mar 06 '23 11:03

Arslan Developer


1 Answers

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

like image 197
Syscall Avatar answered Mar 19 '23 07:03

Syscall