preg_replace
If matches are found, the new subject will be returned, otherwise
subject will be returned unchanged or NULL if an error occurred.
When you check preg_replace('/[^0-9]/', '', $string) == true
it is checking the string '0'
after it has been stripped of all non-numeric characters (still '0'
) and seeing if it evaluates to TRUE
. The string '0'
and the int 0
both evaluate to FALSE
. I.e '0' == FALSE && 0 == FALSE
. This is why your condition is returning FALSE
since '0' == FALSE
.
You can change your condition to this instead: preg_replace('/[^0-9]/', '', $string) != ''
Now you will be checking to see if your string is empty after filtering out all non-numeric characters I.e if your string contained only non-numeric characters it would return FALSE
.