Php 筛选出具有特定模式的字符串开头的数字

Php 筛选出具有特定模式的字符串开头的数字,php,string,filter,numbers,Php,String,Filter,Numbers,我试图从字符串中筛选出一个以@开头的数字。 下面是我认为可以实现的技巧,但它只返回一个空白页 (可能包含很多错误,因为我是PHP新手。) 顺便说一句,您的函数可以简单地用regex()编写。要获取初始字符,请使用$string[0] if (preg_match('/^@(\\d+)/', $string, $results)) { echo $results[1]; } else { if ($string[0] != '@') echo "String doesn'

我试图从字符串中筛选出一个以@开头的数字。 下面是我认为可以实现的技巧,但它只返回一个空白页

(可能包含很多错误,因为我是PHP新手。)



顺便说一句,您的函数可以简单地用regex()编写。要获取初始字符,请使用
$string[0]

if (preg_match('/^@(\\d+)/', $string, $results)) {
   echo $results[1];
} else {
   if ($string[0] != '@')
     echo "String doesn't start with @.";
   else
     echo "The @ isn't followed by a number.";
}

顺便说一句,您的函数可以简单地用regex()编写。要获取初始字符,请使用
$string[0]

if (preg_match('/^@(\\d+)/', $string, $results)) {
   echo $results[1];
} else {
   if ($string[0] != '@')
     echo "String doesn't start with @.";
   else
     echo "The @ isn't followed by a number.";
}
if (preg_match('/^@(\\d+)/', $string, $results)) {
   echo $results[1];
} else {
   if ($string[0] != '@')
     echo "String doesn't start with @.";
   else
     echo "The @ isn't followed by a number.";
}