I have a string that if it is longer than lets say 10 words, I want to split it into two parts. The second part will be included else-where after a 'more' link.
The string will hold html tags too though. 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 woul开发者_StackOverflowd 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. 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. But then you would have problems with the html-tags. Therefore I would cut them as well with something like: substr(strip_tags($text), 0, 50)
. This would allow 50 chars to be displayed, exclusive the html-tags.
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$inAttribute
– whether you're inside a tag attribute (where ">" doesn't end the tag)$currentTagSoFar
– all the characters of the current tag. Would start with "s", then "st", "str", etc., until "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)$wordsSoFar
– the number of words you've found so far- maybe also
$insideComment
, depending on how thorough you want to be
- When you reach your target number of words, pop the tags from the stack and add the remaning closing tags to the string.
精彩评论