2012-08-12 23 views
0

我有相似模式的字符串:PHP - 如何從字符串中提取數字?

john.smith 9.5 9.49296 Active [email protected] +123456789

,我只是想回聲「9.5」出來的是旁邊的「史密斯」使用PHP。

更新:

對不起你們......只注意到它是一個XML文件,無法看到正常的Safari,在Firefox檢查,其顯示爲:

<GetUserInfo> 
<Customer>john.smith</Customer> 
<Balance>9.5</Balance> 
<SpecificBalance>9.49296</SpecificBalance> 
<Status>False</Status> 
<EmailAddress>[email protected]</EmailAddress> 
<Phone>+1234567890</Phone> 
</GetUserInfo> 

現在,這將是PHP代碼回聲「9.5」

感謝您的答案早在... whitespa

+0

所以你需要用php解析這個xml並獲得平衡? – themis 2012-08-12 08:54:05

回答

0

這將工作

<?php 
$string = "<GetUserInfo> 
<Customer>john.smith</Customer> 
<Balance>9.5</Balance> 
<SpecificBalance>9.49296</SpecificBalance> 
<Status>False</Status> 
<EmailAddress>[email protected]</EmailAddress> 
<Phone>+1234567890</Phone> 
</GetUserInfo>"; 

$xml = simplexml_load_string($string); 

echo $xml->Balance; 

?> 
+0

我在這裏找到它http://php.net/manual/en/function.simplexml-load-string.php – themis 2012-08-12 09:01:41

+0

yes..it偉大的作品.... 非常感謝.. – sohal07 2012-08-12 10:30:37

4

嘗試拆分CE(似乎是由被分隔)

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

一個快速的Durty方式

<?php 
$string="john.smith 9.5 9.49296 Active [email protected] +123456789"; 
$array = explode(" ",$string); 
echo $array[1]; 
?> 
+0

我希望我知道常規表達式,我相信有更好的方法 – themis 2012-08-12 07:55:16

+0

如果它每次都遵循相同的模式,我們都選擇(同時)的方法工作得很好。 – 2012-08-12 08:08:34

+0

好,如果字符串格式保持不變,我想不出別的什麼 – themis 2012-08-12 08:14:05

1
$string="john.smith 9.5 9.49296 Active [email protected] +123456789"; 

$array=explode(" ",$string); 

然後你的電話號碼是:

echo $array[1];