號我有一個像它下面有一個字符串有兩個數字提取2從一個字符串中PHP
EUR 66,00 + EUR 3,90 Versandkosten
我需要提取兩個數字前的字符串變量 - 無論是66,00和3,98分別向兩個變量。誰能告訴如何做到這一點
號我有一個像它下面有一個字符串有兩個數字提取2從一個字符串中PHP
EUR 66,00 + EUR 3,90 Versandkosten
我需要提取兩個數字前的字符串變量 - 無論是66,00和3,98分別向兩個變量。誰能告訴如何做到這一點
我需要提取兩個數字 - 6600和3,98分別爲兩個變量。誰能告訴如何做到這一點
有很多,很多(和很多)的方式來做到這一點在PHP中。這裏有一對夫婦。
1. sscanf($subject, 'EUR %[0-9,] + EUR %[0-9,]', $one, $two);
2. preg_match_all('/[\d,]+/', $subject, $matches); list($one, $two) = $matches[0];
如果字符串總是看起來像這樣的正則表達式,像這樣應該努力:
$string = "EUR 66,00 + EUR 3,90 Versandkosten";
preg_match("/([0-9,]+).+([0-9,]+)/", $string, $matches);
var_dump($matches[1], $matches[2]);
preg_match('#([0-9,]+).*?([0-9,]+)#', $String, $Matches);
你的號碼將在$Matches[1]
和$Matches[2]
this也得到數組([0] => 66,00 + EUR 3,90 [1] => 66,00 [2] => 3,90) – 2012-07-25 18:58:10
是的,我知道。你在這裏有什麼意思? – mdziekon 2012-07-25 18:58:50
只需要66,00和3,90相似的數字, – 2012-07-25 19:01:32
這是正確的一個:
<pre>
<?php
// 1The given string
$string = 'EUR 66,00 + EUR 3,90 Versandkosten';
// 2Match with any lowercase letters
$pattern[0] = '/[a-z]/';
// 3Match with any uppercase letters
$pattern[1] = '/[A-Z]/';
// 4Match with any commas
$pattern[2] = '/(,)/';
// 5Match with any spaces
$pattern[3] = '/()/';
// 6 Remove the matched strings
$stripped = preg_replace($pattern,'',$string);
// Split into array from the matched non digit character + in this case.
$array = preg_split('/[\D]/',$stripped);
print_r($array);
?>
</pre>
這個retuns Array([0 ] => 6600 [1] => 390) 錯過了,:) – 2012-07-25 19:00:09
你可以刪除$ pattern [2] ='/(,)/'; – 2012-07-25 19:01:18
酷伴侶,謝謝 – 2012-07-25 19:06:28
考慮這個字符串
$string = 'EUR 66,00 + EUR 3,90 Versandkosten';
$ar=explode($string,' ');
$a=$ar[1];
$b=$ar[4];
第二個是完美的工作 – 2012-07-25 19:40:31