[英]php regex via using preg_match
I have a variable $play
which is made up of characters and numbers. 我有一个变量$play
,它由字符和数字组成。 For example: 例如:
$play = "blah05"
how do i use preg_match()
to only get 05? 我如何使用preg_match()
仅获得05? is my $regexp
correct? 我的$regexp
正确吗?
$regexp = "/^[^0-9]/";
if (preg_match($regexp, $play, $matches)) {
echo "matches[0]";
}
if you want to get numbers from the end, regexp should be this: 如果您想从头获得数字,则regexp应该是这样的:
$regexp = '/(\\d+)$/';
if you want to get numbers from anywhere in the string: 如果要从字符串的任何地方获取数字:
$regexp = '/(\\d+)/';
and just do var_dump($matches);
然后做var_dump($matches);
to see the results and choose which option is best for you. 查看结果并选择最适合您的选项。
if (preg_match($regexp, $play, $matches)) {
var_dump($matches);
}
Try: 尝试:
$regexp = "/([0-9]+)$/";
if (preg_match($regexp, $play, $matches)) {
echo $matches[1];
}
This captures the ones which have the number at the end of the string remove the $ if you want to capture the ones which have the number anywhere in the string. 如果要捕获在字符串中任何位置都有数字的字符串,则捕获$字符串末尾具有数字的字符串。
$regexp = "/[0-9]+/";
if (preg_match($regexp, $play, $matches)) {
echo $matches[0];
}
[0-9] are the numbers. [0-9]是数字。 And with + you match at least one number (as much as possible). 用+匹配至少一个数字(尽可能匹配)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.