简体   繁体   English

在很多单词后分割html字符串

[英]Splitting html string after so many words

I have a string that if it is longer than lets say 10 words, I want to split it into two parts. 我有一个字符串,如果该字符串的长度大于10个单词,我想将其分为两部分。 The second part will be included else-where after a 'more' link. 第二部分将包含在其他位置,在“更多”链接之后。

The string will hold html tags too though. 该字符串也将包含html标签。 For an example the string could be: 例如,字符串可以是:

<p>This is just a test string with more words than the <strong>amount allow</strong> before split, blah blah blah</p>

So in the case I would want: 因此,在这种情况下,我想要:

$string[0] // <p>This is just a test string with more words than</p>;
$string[1] // <p>the <strong>amount allow</strong> before split, blah blah blah</p>;

Thanks in advance 提前致谢

Well, I've had the same issues. 好吧,我遇到了同样的问题。 I solved this in letting my news-writers allow to use "[intro]...[/intro]"-Tags within their texts. 我解决了这个问题,让我的新闻撰稿人允许在其文本中使用“ [intro ... [/ intro]”-Tags)。 Then I parsed the tags with a regex. 然后,我用正则表达式解析标签。

If the cutting should be done automatically without using special tags, it's a bit more difficult. 如果应该在不使用特殊标签的情况下自动进行切割,则难度会更大。 You could use the substr() -function. 您可以使用substr()函数。 But then you would have problems with the html-tags. 但是,那么您将在html标签上遇到问题。 Therefore I would cut them as well with something like: substr(strip_tags($text), 0, 50) . 因此,我也会用类似以下内容的方法来削减它们: substr(strip_tags($text), 0, 50) This would allow 50 chars to be displayed, exclusive the html-tags. 这将允许显示50个字符,不包括html标签。

Maybe this can help you :) 也许这可以帮助您:)

It's not trivial, but here's an idea: 这不是小事,但是有一个主意:

  • Iterate through the string, character by character 逐个字符地遍历字符串
  • keep at least these state variables: 至少保留以下状态变量:
    • $inTag – whether you're inside a tag $inTag –是否在标签内
    • $inAttribute – whether you're inside a tag attribute (where ">" doesn't end the tag) $inAttribute –是否在标签属性内(其中“>”不结束标签)
    • $currentTagSoFar – all the characters of the current tag. $currentTagSoFar –当前标签的所有字符。 Would start with "s", then "st", "str", etc., until "strong" 将以“ s”开头,然后是“ st”,“ str”等,直到“ strong”
    • $openedTags – stack variable where you put the currently opened tags (push when you find an opening tag, pop when you find a closing one) $openedTags –堆栈变量,用于放置当前打开的标签(在找到开始标签时按下,在找到结束标签时弹出)
    • $wordsSoFar – the number of words you've found so far $wordsSoFar –到目前为止找到的单词数
    • maybe also $insideComment , depending on how thorough you want to be 也许也$insideComment ,这取决于您想要多彻底
  • When you reach your target number of words, pop the tags from the stack and add the remaning closing tags to the string. 当达到目标单词数时,从堆栈中弹出标签,然后将剩余的结束标签添加到字符串中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM