任务是将字符串拆分为500个字符到数组中。我用str_split完成了这个,但是我遇到了问题。当然,它必须用文字吐出,否则这个文字是不可读的。然后更多。这个文本带有链接,如果我拆分链接将会被破坏(实际上是任何html)=)所以我只需要在标签结束或者甚至没有开始时才开始拆分......同样的话。 ±100个字符不是问题。
我真的很感激这段代码。我对regexp不太满意。
编辑:示例
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]。如果它是那个地方的链接,它就会被破坏。
答案 0 :(得分:1)
最好不要使用正则表达式,而是使用PHP的原生XML / HTML解析功能。类似下面的代码可能会做你想要的:
<?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.";
$dom = new DOMDocument;
$root = $dom->createDocumentFragment();
$root->appendXML($str);
$bits = array();
foreach ($root->childNodes as $node) {
if ($node->nodeType == XML_TEXT_NODE) {
$bits = array_merge($bits, explode(' ', $node->nodeValue));
} elseif ($node->nodeType == XML_ELEMENT_NODE) {
$dom->appendChild($newnode = $node->cloneNode(true));
$bits[] = $dom->saveHTML();
$dom->removeChild($newnode);
}
}
var_dump($bits);
答案 1 :(得分:1)