【问题标题】:Split text containing numbered lines into array of sentences将包含编号行的文本拆分为句子数组
【发布时间】:2022-11-04 20:57:05
【问题描述】:

如何将此编号段落转换为句子数组?

1. The best foods for your cat's diet
2. How to ensure your cat is getting enough nutrients
3. The importance of a balanced diet for cats
4. How to make sure your cat is getting enough water
5. Tips for dealing with finicky eaters
6. How to tell if your cat is overweight
7. The dangers of overfeeding your cat5

期望的输出:

$sentences = [ 
    "The best foods for your cat's diet",
    "How to ensure your cat is getting enough nutrients",
    "The importance of a balanced diet for cats",
    "How to make sure your cat is getting enough water",
    "Tips for dealing with finicky eaters",
    "How to tell if your cat is overweight",
    "The dangers of overfeeding your cat5"
];

【问题讨论】:

  • @DarkBee 它为我工作,如何从句子中删除序列号?像 1. , 2. , 3. .... 我没有找到确切的输出...

标签: php arrays regex preg-split


【解决方案1】:

拆分文本:

  • 可选的前导换行序列(R*R?
  • 一个数字 (d+)
  • 一个点 (.)
  • 然后是一个空格 ( )

代码:(Demo)

var_export(
    preg_split(
        '/R*d+. /',
        $text,
        0,
        PREG_SPLIT_NO_EMPTY
    )
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多