Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 引爆价格符号&;号码_Php_Regex - Fatal编程技术网

Php 引爆价格符号&;号码

Php 引爆价格符号&;号码,php,regex,Php,Regex,如何分解$123或USD123或CAD$123或元123动态进入 array [0] = "$" array [1] = 123 ? 上面是我的测试代码,但我无法在数组[0]中获取$,知道吗 谢谢,试试这个,它对所有人都有用 $str = 'CAD$123'; $num = filter_var($str, FILTER_SANITIZE_NUMBER_INT); $curr = explode($num,$str); echo 'Your currency is '.$curr[0].'

如何分解
$123
USD123
CAD$123
元123
动态进入

array [0] = "$"
array [1] = 123
?

上面是我的测试代码,但我无法在
数组[0]
中获取
$
,知道吗


谢谢,试试这个,它对所有人都有用

$str = 'CAD$123';
$num = filter_var($str, FILTER_SANITIZE_NUMBER_INT);
$curr = explode($num,$str);

echo 'Your currency is '.$curr[0].' and number is '.$num;
细分:

^(\D+)
在字符串开头匹配一个或多个非数字

(\d+)$
匹配一个或多个数字,直到字符串结束

$txt='$123';

  $re1='(\\$)'; # Any Single Character 1
  $re2='(\\d+)';    # Integer Number 1

  if ($c=preg_match_all ("/".$re1.$re2."/is", $txt, $matches))
  {
      $c1=$matches[1][0];
      $int1=$matches[2][0];
      print "($c1) ($int1) \n";
  }

您可以将preg_匹配与以下正则表达式一起使用

preg_match('/([^0-9]*)([0-9\.]*)(.*)?/', '$20.99USD', $match);
var_dump($match);
以上将产生

Array
(
    [0] => $20.99USD
    [1] => $
    [2] => 20.99
    [3] => USD
)
它可以将$、20.99、USD解析为数组中的单独索引

preg_match('/([^0-9]*)([0-9\.]*)(.*)?/', '$20.99USD', $match);
var_dump($match);
Array
(
    [0] => $20.99USD
    [1] => $
    [2] => 20.99
    [3] => USD
)