使用preg_match在字符串中查找百分比值 [英] Find a percentage value in a string using preg_match
本文介绍了使用preg_match在字符串中查找百分比值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试隔离文本字符串中的百分比值.使用preg_match应该很容易,但是因为在preg_match中使用百分号作为运算符,所以我无法通过搜索找到任何示例代码.
I'm trying to isolate the percentage value in a string of text. This should be pretty easy using preg_match, but because the percentage sign is used as an operator in preg_match I can't find any sample code by searching.
$string = 'I want to get the 10% out of this string';
我想结束的是:
$percentage = '10%';
我的猜测是我需要类似的东西:
My guess is that I'll need something like:
$percentage_match = preg_match("/[0-99]%/", $string);
我确定对此有一个非常快速的答案,但是解决方案使我无所适从!
I'm sure there is a very quick answer to this, but the solution is evading me!
推荐答案
if (preg_match("/[0-9]+%/", $string, $matches)) {
$percentage = $matches[0];
echo $percentage;
}
这篇关于使用preg_match在字符串中查找百分比值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文