删除preg拆分后的标点符号


Remove punctuation mark after the preg split

我使用此代码在第一个标点符号后拆分内容,检索第一个句子。

$content = preg_split('/(?<=[!?.])./', $content);

如何删除拆分句子末尾剩余的标点符号?

怎么样:

$content = "Hello, world! What's new today? Everything's OK.";
$arr = preg_split('/[!?.] ?/', $content, -1, PREG_SPLIT_NO_EMPTY);
print_r($arr);

这在标点符号!?.后面跟一个可选空格上进行拆分。捕获的字符串不包含前导空格。

与你的不同之处在于我听不懂标点符号。

输出:

Array
(
    [0] => Hello, world
    [1] => What's new today
    [2] => Everything's OK
)

您可以在此之后运行

$content = ltrim($content, '.');

$content = str_replace('.', '', $content);

您可以使用:

$content = substr($content, 0, -1);

这将删除$content中的最后一个字符。

$content = preg_split('/[!?.]/', $content, null, PREG_SPLIT_NO_EMPTY);

尝试dis代码!

$test   =   'Ho! My! God!';
    $temp   =   split('!',trim($test,'!'));
    echo "=".__LINE__."=><pre>";print_r($temp);echo "</pre>";
    Array
(
    [0] => Ho
    [1] =>  My
    [2] =>  God
)