我如何在 php 中分隔一个句子?

How i can separate a sentence in php?

我有一个句子 [1234]-blablabla,我只想保留数字 1234。有人可以告诉我如何实现这一目标吗?

试试这个

    $str = '[1234]-blablabla';
    preg_match_all('([\d]+)', $str, $matches);
    print_r($matches);

输出

array(2
0   =>  1234
1   =>  1234
)

点这里 http://www.phpliveregex.com/

试试这个:-

$str = '[1234]-blablabla';
preg_match_all('!\d+!', $str, $matches);
echo "<pre/>";print_r($matches); // print as an array
$new_string = $matches[0][0]; // assign to an string variable

echo $new_string; // print that string variable.

输出:- http://prntscr.com/7ano0t

注意:- 这取决于您想要的方式。数组或字符串。为了您的方便,我把两者都放了。谢谢。