开发者

How to select start of a string until a :

I have this string:

467:some-text-here-1786

How can I select only the first numerical value befo开发者_Python百科re the ":" ?

Thank you


Very simple:

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

or

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

or (as pointed out by PhpMyCoder)

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


$string = '467:some-text-here-1786';
$var = (int)$string;

Since you're extracting a number, this is enough :) For an explanation of why this works, check the official PHP manual: http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion

It's also really fast and really safe: you are sure you get a number.


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


another way to do this:

$length = strpos($string, ':') + 1;
$number = substr($string, 0, $length);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜