对不起我的英语不好。我有一个小问题:有一个变量,例如:
$a = '123-abc';
我的问题是,我怎样才能得到变量 $a 中的数字 123 ?
感谢您的帮助:D
// what you want is $ret[0]
$ret = explode('-', $a);
echo $ret[0];
substr($a, 0, strpos($a, '-'));
或者
preg_match('~^[^-]+~', $a, $matches);
var_dump($matches);
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/