character-based pagination - inserting page breaks on text, not punctuation or code
<p>I'm writing code to generate character-based pagination. I have articles in my site that I want to split up based on length.</p>

<p>The code I have so far is working albeit two issues:</p>

<ol>
<li>It's splitting pages in the middle of words and HTML tags; I want it to
only split after a complete word, tag, or a punctuation mark. </li>
<li>In the pagination bar, it's generating the wrong number of pages.</li>
</ol>

<p>In the
    pagination bar, it's generating the
    wrong number of pages.</p>

<p>Need help addressing these two issues. Code follows:</p>

<pre><code>$text = file_get_contents($View);
$ArticleLength = strlen($text);
$CharsPerPage = 5000;
$NoOfPages = round((double)$ArticleLength / (double)$CharsPerPage);
$CurrentPage = $this-&gt;ReturnNeededObject('pagenumber');
$Page = (isset($CurrentPage) &amp;&amp; '' !== $CurrentPage) ? $CurrentPage : '1';
$PageText = substr($text, $CharsPerPage*($Page-1), $CharsPerPage);
echo $PageText, '&lt;p&gt;';
for ($i=1; $i&lt;$NoOfPages+1; $i++)
{
    if ($i == $CurrentPage)
    {
        echo '&lt;strong&gt;', $i, '&lt;/strong&gt;';
    }
    else
    {
         echo '&lt;a href="', $i, '"&gt;', $i, '&lt;/a&gt;';
    }
    echo ' | ';
}
echo '&lt;/p&gt;';
</code></pre>

<p>What am I doing wrong?</p>
