PHP regular expressions: No ending delimiter '^' found in

前端 未结 3 802
无人及你
无人及你 2020-11-22 09:04

I\'ve been having some trouble with regular expressions.

This is my code

$pattern = \"^([0-9]+)$\";

if (preg_match($pattern, $input))
   echo \"yes         


        
相关标签:
3条回答
  • 2020-11-22 09:08

    PHP regex strings need delimiters. Try:

    $numpattern="/^([0-9]+)$/";
    

    Also, note that you have a lower case o, not a zero. In addition, if you're just validating, you don't need the capturing group, and can simplify the regex to /^\d+$/.

    Example: http://ideone.com/Ec3zh

    See also: PHP - Delimiters

    0 讨论(0)
  • 2020-11-22 09:23

    Your regex pattern needs to be in delimiters:

    $numpattern="/^([0-9]+)$/";
    
    0 讨论(0)
  • 2020-11-22 09:32

    You can use T-Regx library, that doesn't need delimiters

    pattern('^([0-9]+)$')->match($input);
    
    0 讨论(0)
提交回复
热议问题