Dar*_*ney 2 php regex preg-match-all
我preg_match_all($pattern, $string, $matches)经常使用数据,基本上,我总是需要从数组中删除0.00英镑或0.00美元或0.00欧元.
如果有的话,我永远不知道它将在阵列中的位置.
我尝试过unset($matches['£0.00']),unset($matches[0]['£0.00'])但没有奏效
此外,它可以在不使用实际货币符号和正则表达式的情况下完成 \p{Sc}
所以,总结一下,我有一个带货币符号的数字数组,需要删除任何零条目.
样本数组:
Array ( [0] => Array ( [0] => £18.99 [1] => £0.00 [2] => £0.00 [3] => £0.00 ) )
Run Code Online (Sandbox Code Playgroud)
这可能吗?
使用preg_match()和preg_match_all()有时可能很奇怪
$matches 将是模式的匹配数组 - 在这种情况下,如果你
preg_match("/\p{Sc}0\.00/","£0.00",$matches);
//$matches[0] => £0.00
Run Code Online (Sandbox Code Playgroud)
或者如果()在模式中添加括号,则可以提取零件
preg_match("/\p{Sc}(0\.00)/","£0.00",$matches);
//$matches[0] => £0.00
//$matches[1] => 0.00
Run Code Online (Sandbox Code Playgroud)
所以要解决这个问题试试这个
foreach($sample_array as $key => $value)
{
if(preg_match("/(\$|\p{Sc})0\.00/",$value))
{
unset($sample_array[$key]);
}
}
Run Code Online (Sandbox Code Playgroud)
注意|表示的管道$或者£- 如果你为欧元符号添加另一个管道它也会捕获它 - 同时注意我们unset()在原始数组中键入一个键而不是 - unset()在$matches数组中