我正在尝试隔离文本字符串中的百分比值。使用 preg_match 应该很容易,但是由于百分号在 preg_match 中用作运算符,我无法通过搜索找到任何示例代码。
$string = 'I want to get the 10% out of this string';
我想要结束的是:
$percentage = '10%';
我猜我需要这样的东西:
$percentage_match = preg_match("/[0-99]%/", $string);
我确信对此有一个非常快速的答案,但解决方案正在逃避我!
最佳答案
if (preg_match("/[0-9]+%/", $string, $matches)) {
$percentage = $matches[0];
echo $percentage;
}
关于php - 使用 preg_match 在字符串中查找百分比值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4799671/