如何在symbol'-';在PHP中
对不起我的英语。 我有一个小问题: 有一个变量,例如:如何在symbol'-';在PHP中,php,Php,对不起我的英语。 我有一个小问题: 有一个变量,例如: $a = '123-abc'; 我的问题是,如何在变量$a中获得数字123 谢谢你的帮助:D substr($a, 0, strpos($a, '-')); 或 或 在下面的示例中,我们声明了一个字符串变量,并按以下格式为其分配了一个电话号码:001-234-567678。所以现在我们要循环并获取字符串(电话号码)中每个连字符前面的值 $phone_number=“001-234-567678”; //使用爆炸法 $arr\u ph=爆
$a = '123-abc';
我的问题是,如何在变量$a中获得数字123
谢谢你的帮助:D
substr($a, 0, strpos($a, '-'));
或
或
在下面的示例中,我们声明了一个字符串变量,并按以下格式为其分配了一个电话号码:001-234-567678。所以现在我们要循环并获取字符串(电话号码)中每个连字符前面的值
$phone_number=“001-234-567678”;
//使用爆炸法
$arr\u ph=爆炸(“-”,$phone\u number);
//foreach循环以显示返回的数组
foreach($i){
echo$i.“
”;
}
输出
001
234
567678
更多信息
https://www.jquery-az.com/php-explode-method-to-split-a-string-with-3-examples/
在下面的示例中,我们声明了一个字符串变量,并按以下格式为其分配了一个电话号码:001-234-567678。所以现在我们要循环并获取字符串(电话号码)中每个连字符前面的值
$phone_number=“001-234-567678”;
//使用爆炸法
$arr\u ph=爆炸(“-”,$phone\u number);
//foreach循环以显示返回的数组
foreach($i){
echo$i.“
”;
}
输出
001
234
567678
更多信息
https://www.jquery-az.com/php-explode-method-to-split-a-string-with-3-examples/
是否也值得添加2
作为第三个分解参数是否也值得添加2
作为第三个分解参数
preg_match('~^[^-]+~', $a, $matches);
var_dump($matches);
// what you want is $ret[0]
$ret = explode('-', $a);
echo $ret[0];
In the following example, we have declared a string variable and assigned it a phone number in this format: 001-234-567678. So now we want to loop and get value before each hyphens in string (phone number)
$phone_number = "001-234-567678";
//Using the explode method
$arr_ph = explode("-",$phone_number);
//foreach loop to display the returned array
foreach($arr_ph as $i){
echo $i . "<br />";
}
Output
001
234
567678
For more information
https://www.jquery-az.com/php-explode-method-to-split-a-string-with-3-examples/