`preg_match` the Whole String in PHP

前端 未结 3 688
北海茫月
北海茫月 2021-01-26 04:43

This thing really confuses me, pardon my ignorance.

I have a var $name in here that I want to be free from number, I don\'t want any digit to be included in

3条回答
  •  日久生厌
    2021-01-26 05:24

    Your regex only checks that there is at least one non-digit. Instead, you need to check that it is only non-digits:

    var_dump(preg_match('/^\D+$/',$name));
    

    (^ and $ are the beginning and end of the string. \D means anything not a digit--the opposite of \d. So this only matches non-digits from beginning to end. It doesn't match an empty string. Replace + with * if you want to match an empty string as well).

提交回复
热议问题