如何只获得美元符号后的数字

时间:2014-11-24 21:48:47

标签: php regex

如果$ string类似于sokewe(letter) $ 10,00 seqee(letter)如何只获得美元符号后的数字?例如sokewe(letter) $ 10,00 seqee(letter) sokewe(letter) $ 1000 seqee(letter)

<?php

if (preg_match('/\$\s[0-9]/',$str)) {
  // echo'has $ digit';
  $currency = '$';
  $digit = ..
} else if (preg_match('/\$[0-9]/',$str)) {
  // echo'has $digit';
  $currency = '$';
}

if (preg_match('/\£\s[0-9]/',$str)) {
  // echo'has £ digit';
  $currency = '£';
} else if (preg_match('/\£[0-9]/',$str)) {
  // echo'has £digit';
  $currency = '£';
}

if (preg_match('/\€\s[0-9]/',$str)) {
  // echo'has € digit';
  $currency = '€';
} else if (preg_match('/\€[0-9]/',$str)) {
  // echo'has €digit';
  $currency = '€';
}

5 个答案:

答案 0 :(得分:3)

您可以使用:

if (preg_match('/\$\D*(\d+(?:,\d+)*)/', $input, $matches)) {
   echo $matches[1] . "\n"; // 10,00
}

RegEx Demo

答案 1 :(得分:1)

捕获它:

if (preg_match('/\$\s([0-9])/',$str, $matches)) {
                     ^-----^       ^^^^^^^^^^
   $digit = $matches[1];
}

答案 2 :(得分:0)

您好,您只需要使用php正则表达式

$str = '$10.00';
preg_match('/\d+(,|.)?\d+/',$str,$matches);


print_r($matches);

您可能也想测试一下。以防我有命令(,)和点(。),因为你有$,€,£符号和金额可能用点或逗号表示不同。

如果您想测试它,可以使用以下链接http://leaverou.github.io/regexplained/

答案 3 :(得分:0)

试试这个:

if(preg_match('/([£$€])([0-9]+[.,]?(?:[0-9]{2})?)/', $str, $matches)) {
    $currency = $matches[1];
    $value = $matches[2];
}

答案 4 :(得分:-1)

实施例。 您可以使用substr和strrchr。

  

$ string ='$ 10,00';

     

$ return = substr(strrchr($ string,'$'),1);

     

echo $ return; // 10,00

相关问题