被各种分隔符分割,同时保留分隔符? [英] Split by various delimiters, while keeping the delimiter?

查看:119
本文介绍了被各种分隔符分割,同时保留分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分割文本

过公元年?因为无论你如何选择。简体字危及了对古代文学的研究输入!

使用这三个(或更多)?!。字符作为分隔符。
i当然可以使用

$ lines = preg_split('/[。,!,?]/ u',$ body);

Using on of these three (or more) ?!。 characters as delimiter. i can do this of course with
$lines = preg_split('/[。,!,?]/u',$body);

但是我不想让结果行保留其结束定界符。 啊。。。什么!?? !!!!

However i wan't to have the resulting lines keep their ending delimiter. Also a sentence might end like so 啊。。。 or 什么!??!!!!

推荐答案

尝试一下:

$lines = preg_split('/(?<=[。!?])(?![。!?])/u',$body);

它在一个分隔符的位置分割,但前面没有分隔符。它不占用定界符,并且如果有两个或多个连续定界符,则仅在最后一个定界符之后进行匹配。

It splits at a position that's preceded by one of your delimiter characters but not followed by one. It doesn't consume the delimiter, and if there are two or more consecutive delimiters, it only matches after the last one.

这篇关于被各种分隔符分割,同时保留分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆