PHP - 如何从字符串中提取数字


PHP - How to extract a number from the string?

>我在模式中有一个类似于以下内容的字符串:

john.smith 9.5 9.49296 Active john.s@site.com +123456789

我只想使用 PHP 在"smith"旁边呼应"9.5"。

更新:

对不起伙计们...刚刚注意到它是一个XML文件,在Safari中无法正确看到,在Firefox中检查并显示为:

<GetUserInfo>
<Customer>john.smith</Customer>
<Balance>9.5</Balance>
<SpecificBalance>9.49296</SpecificBalance>
<Status>False</Status>
<EmailAddress>john.s@site.com</EmailAddress>
<Phone>+1234567890</Phone>
</GetUserInfo>

现在回显"9.5"的 php 代码是什么

感谢您之前的回答...

尝试在空格上拆分(似乎由它分隔)

$parts = preg_split('/'s+/', $input);
print $parts[1];

一种快速而持久的方法

<?php
$string="john.smith 9.5 9.49296 Active john.s@site.com +123456789";
$array = explode(" ",$string);
echo $array[1];
?>
$string="john.smith 9.5 9.49296 Active john.s@site.com +123456789";
$array=explode(" ",$string);

那么你的数字将是:

echo $array[1];

这将起作用

<?php
$string = "<GetUserInfo>
<Customer>john.smith</Customer>
<Balance>9.5</Balance>
<SpecificBalance>9.49296</SpecificBalance>
<Status>False</Status>
<EmailAddress>john.s@site.com</EmailAddress>
<Phone>+1234567890</Phone>
</GetUserInfo>";
$xml = simplexml_load_string($string);
echo  $xml->Balance;
?>