我有以下文本作为示例:
\n\t\t\t\t\t3 comments
谁能帮我构建用于提取数字的 PHP 正则表达式公式? (在本例中 number = 3
)
该号码可以以任何文本作为前缀,但号码后面应该有 <space>
接下来是 comments
。
谢谢。
最佳答案
$text = '\n\t\t\t\t\t3 comments';
if (preg_match('/(\d+)\scomments/', $text, $match)) {
$number = $match[1];
} else {
// the number was not found
}
这将匹配字符串中存在的任何数字,并将其分配给变量$number
。如果没有找到该号码,可以在else
语句中处理。
关于php - 需要帮助在 PHP 中构建正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6922866/