开发者

How to select start of a string until a :

开发者 https://www.devze.com 2023-04-02 16:12 出处:网络
I have this string: 467:some-text-here-1786 How can I select only the first numerical value befo开发者_Python百科re the \":\" ?

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

精彩评论

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