PHP preg_split()模式用于按句周期拆分,但在浮动/价格期间会出现分割。

时间:2022-01-13 22:08:11

I would like to be able to preg_split content by periods after sentences i.e.

我希望能够在句子后的句点上对内容进行预分解。

Lorem ipsum dolor sit 3.14 amet, elit. Vivamus sed elit eu. Morbi pulvinar dignissim.

Lorem ipsum dolor sit 3.14 amet, elit。Vivamus sed elit欧盟。发病原因枕dignissim。

should output (dots in floats shouldn't be split):

应输出(浮点数不应分割):

array(
  'Lorem ipsum dolor sit 3.14 amet, elit',
  'Vivamus sed elit eu',
  'Morbi pulvinar dignissim'
)

not

array(
  'Lorem ipsum dolor sit 3',
  '14 amet, elit',
  'Vivamus sed elit eu',
  'Morbi pulvinar dignissim'
)

any ideas how the preg_split pattern should looks like? cheers

关于preg_split模式应该是什么样的想法?干杯

3 个解决方案

#1


3  

This one may work

这个可以工作

$res = preg_split('/\.[^\d]/', $str);

#2


1  

The following works on your example, but I'm not sure it'll always do the job, but hope you can use it: "/\.[^$|\d]/"

下面的工作在你的例子中,但我不确定它会一直做这项工作,但希望你可以使用它:“/ \[^ $ | \ d]/。”

#3


0  

In case you only want to split in case there's a space or end of string after the dot:

如果你只是想在一个空格或字符串后面加上一个空格:

$res = preg_split('~\.( |$)~', $str);

This can give you empty results, which you can drop by setting the PREG_SPLIT_NO_EMPTY flag.

这会给您空的结果,您可以通过设置PREG_SPLIT_NO_EMPTY标志来删除它。

$res = preg_split('~\.( |$)~', $str, 0, PREG_SPLIT_NO_EMPTY);

See as well preg_split.

看到preg_split。

#1


3  

This one may work

这个可以工作

$res = preg_split('/\.[^\d]/', $str);

#2


1  

The following works on your example, but I'm not sure it'll always do the job, but hope you can use it: "/\.[^$|\d]/"

下面的工作在你的例子中,但我不确定它会一直做这项工作,但希望你可以使用它:“/ \[^ $ | \ d]/。”

#3


0  

In case you only want to split in case there's a space or end of string after the dot:

如果你只是想在一个空格或字符串后面加上一个空格:

$res = preg_split('~\.( |$)~', $str);

This can give you empty results, which you can drop by setting the PREG_SPLIT_NO_EMPTY flag.

这会给您空的结果,您可以通过设置PREG_SPLIT_NO_EMPTY标志来删除它。

$res = preg_split('~\.( |$)~', $str, 0, PREG_SPLIT_NO_EMPTY);

See as well preg_split.

看到preg_split。