因此,我确实不熟悉正则表达式,但我试图从字符串中提取货币。
我已经尝试过一堆据说" for#34; PHP,但似乎没有任何工作。所以我回到了基础。
模式
\$\d*
字符串
2009 Low Price -- One Owner $8800 OBO
我能够通过http://www.phpliveregex.com/p/6eJ
测试/验证此模式但是当我跑步时
$pattern = "/\$\d*/";
$match = preg_match_all($pattern, $this->Title, $matches);
echo $matches[0][0];
我没有结果......我也尝试过使用
$pattern = "/\\$\d*/";
和
$pattern = "/\\$\\d*/";
任何人都知道为什么会这样吗?或者我错过了一些非常明显的东西?
修改
在OSX上使用PHP 5.5.11我认为它是一个特定的实例..?
答案 0 :(得分:0)
尝试此模式以匹配货币值
~\$\d+~
您的代码将是,
<?php
$data = "2009 Low Price -- One Owner $8800 OBO";
$regex = '~\$\d+~';
preg_match_all($regex, $data, $matches);
var_dump($matches);
?>
输出:
array(1) {
[0]=>
array(1) {
[0]=>
string(5) "$8800"
}
}