gpt4 book ai didi

PHP:按单词和标签将字符串准确拆分为数组

转载 作者:搜寻专家 更新时间:2023-10-31 21:17:51 24 4
gpt4 key购买 nike

任务是将字符串按 500 个字符拆分为数组。我已经用 str_split 完成了这个,但是我遇到了一个问题。当然必须用文字来吐槽,否则这段文字是不可读的。不仅如此。此文本带有链接,如果我将它们拆分(实际上是任何 html),链接将被破坏 =) 因此,仅当标记结束甚至尚未开始时,我才需要开始拆分......同样适用于文字。 ±100 个字符不是问题。

我真的很感激一段代码来做到这一点。我不太擅长正则表达式。

编辑:示例

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac diam non nisl interdum tempus. Nam id ipsum id nunc tempus varius. Suspendisse ut neque a velit elementum placerat. Curabitur lobortis, lorem sit <a href="#">amet tincidunt ultricies,</a> eros ante feugiat dui, sit amet lacinia metus risus a magna. Duis velit dui, sollicitudin at aliquet et, elementum at dui. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;

脚本:

<?php

$str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. <a href=\"http://example.com\">Phasellus condimentum
facilisis ipsum</a>, quis elementum urna ornare non. Cras nisi libero, dapibus sed euismod id, pharetra eu libero.
Maecenas mi nulla, ultrices in congue in, viverra ac massa. Quisque <br/>at turpis nulla. Suspendisse semper urna eu
augue aliquet dictum. Mauris at purus in lectus varius bibendum. <em>Fusce hendrerit <strong>posuere ante</strong></em>,
at pellentesque odio lobortis at. Integer quis urna eget ipsum dictum volutpat quis et leo. Etiam hendrerit eleifend
ornare. Phasellus eget justo elit.";

$str = str_split($str, 200);

var_dump($str);

输出:

    array(4) {
[0]=>
string(200) "Lorem ipsum dolor sit amet, consectetur adipiscing elit. <a href="http://example.com">Phasellus condimentum
facilisis ipsum</a>, quis elementum urna ornare non. Cras nisi libero, dapibus sed euismod "
[1]=>
string(200) "id, pharetra eu libero.
Maecenas mi nulla, ultrices in congue in, viverra ac massa. Quisque <br/>at turpis nulla. Suspendisse semper urna eu
augue aliquet dictum. Mauris at purus in lectus varius bi"
[2]=>
string(200) "bendum. <em>Fusce hendrerit <strong>posuere ante</strong></em>,
at pellentesque odio lobortis at. Integer quis urna eget ipsum dictum volutpat quis et leo. Etiam hendrerit eleifend
ornare. Phasellus"
[3]=>
string(17) " eget justo elit."
}

这是一个严格的字符拆分,一半的单词出现在 $str[1] 上。如果它是那个地方的链接,它就会被破坏。

最佳答案

关于PHP:按单词和标签将字符串准确拆分为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4638790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com