Check if variable has a number php

后端 未结 5 1620
感动是毒
感动是毒 2021-01-30 15:09

I want to check if a variable has a number in it, I just want to see if there is one I don\'t care if it has any thing else in it like so:

\"abc\" - false
\"!./#         


        
5条回答
  •  感情败类
    2021-01-30 15:20

    You can use the strcspn function:

    if (strcspn($_REQUEST['q'], '0123456789') != strlen($_REQUEST['q']))
      echo "true";
    else
      echo "false";
    

    strcspn returns the length of the part that does not contain any integers. We compare that with the string length, and if they differ, then there must have been an integer.

    There is no need to invoke the regular expression engine for this.

提交回复
热议问题