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 '下划线' 面前获得数]
相关问题