Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何选择字符串的开头,直到:_Php - Fatal编程技术网

Php 如何选择字符串的开头,直到:

Php 如何选择字符串的开头,直到:,php,Php,我有这个字符串: 467:some-text-here-1786 如何仅选择以下值之前的第一个数值: 谢谢非常简单: list($var) = explode(":",$input); 或 或者正如PhpMyCoder所指出的那样 $tmp = current(explode(":",$input)); 既然您正在提取一个数字,这就足够了: 有关这一工作原理的解释,请查看官方PHP手册: 它也非常快速和安全:你肯定能得到一个号码。另一种方法是: $a = "467:some-text-her

我有这个字符串:

467:some-text-here-1786

如何仅选择以下值之前的第一个数值:

谢谢

非常简单:

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

或者正如PhpMyCoder所指出的那样

$tmp = current(explode(":",$input));
既然您正在提取一个数字,这就足够了: 有关这一工作原理的解释,请查看官方PHP手册:


它也非常快速和安全:你肯定能得到一个号码。

另一种方法是:

$a = "467:some-text-here-1786";
$a = explode(":", $a);
$a = $a[0];
$length = strpos($string, ':') + 1;
$number = substr($string, 0, $length);

可能重复的可能重复的可能重复的可能重复的更多您也可以使用reset而不是array\u shift。使用array\u shift将失败,因为它期望参数1作为引用。您需要使用一个临时变量。
$a = "467:some-text-here-1786";
$a = explode(":", $a);
$a = $a[0];
$length = strpos($string, ':') + 1;
$number = substr($string, 0, $length);