Showing posts with label php split sentences in each paragraph and wrap in span. Show all posts
Showing posts with label php split sentences in each paragraph and wrap in span. Show all posts

split sentences in each paragraph and wrap in span

Wednesday, November 4, 2015

I'm using this to split articles into individual sentences so I can highlight each sentence on mouseover:



$a_text = str_replace('<p>', '', $a_text);
$a_text = explode('</p>', $a_text); // Separate into paragraphs
foreach($a_text as $p) {
$p_text = explode('. ', $p); // Only spilt on periods followed by a space to preserve URLs in the article.
$highlight_text .= '<p>';
$i = 1;
foreach($p_text as $text) {
$highlight_text .= '<span class="highlight" alt="'.$i.'">'.$text.'.</span> '; // Wrap each sentence in the highlight class
$i++;
}
$highlight_text .= '</p>';
}
echo $highlight_text;


My problem is that at the end of every paragraph, there is a double period. It will look something like this:




Sentence one. Sentence two. Sentence three. .




Nothing I do removes that trailing space + period from the end of every paragraph, and I can't figure out how it's getting applied.