2011-09-06 57 views
0

我有這個字符串:如何選擇一個字符串的開始,直到:

467:一些文本,在這裏,1786

我怎麼能只選擇前第一數值「:」?

謝謝

+0

可能重複(http://stackoverflow.com/questions/2220998/get-number -in-front-of-underscore-with-php) – Gordon

+0

[在php中首次出現字符之前返回字符串部分](http://stackoverflow.com/questions/3766301/return-the - 在字符串中的第一個字符出現之前的一個字符串部分) – Gordon

+0

[如何在字符串中提取@前的所有文本](http:// stackoverflow.com/questions/6273679/how-to-extract-all-text-in-fr字符串中的字符) – Gordon

回答

6

很簡單:

list($var) = explode(":",$input); 

$tmp = explode(":",$input); 
$var = array_shift($tmp); 

或(如PhpMyCoder指出)

$tmp = current(explode(":",$input)); 
+1

+1使用列表。 – ngen

+0

thanx @Kokos :) –

+1

您也可以使用'reset()'而不是'array_shift()'。 –

1
$a = "467:some-text-here-1786"; 
$a = explode(":", $a); 
$a = $a[0]; 
0

另一種方式來做到這一點:

$length = strpos($string, ':') + 1; 
$number = substr($string, 0, $length); 
的[在PHP '下劃線' 面前獲得數]
相關問題