抱歉我的英文。 我有一个小问题: 有一个变量,如:
$a = '123-abc';
我的问题是,如何在变量$ a中得到数字123?
感谢您的帮助:D
答案 0 :(得分:4)
// what you want is $ret[0]
$ret = explode('-', $a);
echo $ret[0];
答案 1 :(得分:2)
substr($a, 0, strpos($a, '-'));
或
preg_match('~^[^-]+~', $a, $matches);
var_dump($matches);
答案 2 :(得分: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/