1

对不起我的英语不好。我有一个小问题:有一个变量,例如:

$a = '123-abc';

我的问题是,我怎样才能得到变量 $a 中的数字 123 ?

感谢您的帮助:D

4

3 回答 3

4
// what you want is $ret[0]
$ret = explode('-', $a);
echo $ret[0];
于 2012-06-13T03:22:21.210 回答
2
substr($a, 0, strpos($a, '-'));

或者

preg_match('~^[^-]+~', $a, $matches);
var_dump($matches);
于 2012-06-13T03:21:05.017 回答
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/
于 2018-10-01T05:32:45.930 回答