如何解析“N/A - -0.09%”?并在PHP中提取第一个连字符后的数字


How do I parse "N/A - -0.09%" and extract the number after the first hyphen in PHP?

这是我的字符串:"N/A - -0.09%"(包括双引号和我想要剖析的字符串的一部分)

下面是我的代码:

$url = 'abc123.php';    
$data = file_get_contents($url);  //$data contains "N/A - -0.09%" (the string to parse)
$regex = '/""/';
preg_match($regex,$data,$match);
var_dump($match);

如何隔离第一个连字符之后,但在%符号之前的所有内容?

我知道一个事实,字符串将始终有"N/a -"在前面,我只想提取其负号的数字,如果它是负的。

我想给一个变量赋值-0.09。这个数字可以是正的,也可以是负的。如果它是正数,就没有连字符。0.123. 如果是负数,除了第一个连字符外,还会有第二个连字符,例如。-2.5 .

请帮我制定regex部分隔离-0.09到一个变量,说$number。非常感谢!

我不会在这里使用正则表达式,我只是删除两个引号(使用str_replace()代替没有"),然后将字符串分成单词(使用explosion()与' '作为分隔符),然后使用array_pop()抓取最后一个"单词"。

url = 'abc123.php';    
$data = file_get_contents($url);  //$data contains "N/A - -0.09%" (the string to parse)
$match = array_pop(explode(' ', str_replace("'"", '', $data)));
echo $match . "'n";