title: Puzzle: Splitting An HTML String Correctly
body: <p>I'm trying to split an HTML string by a token in order to create a blog preview without displaying the full post. It's a little <em>harder</em> than I first thought. Here are the problems:</p>
  
  <ul>
  <li>A user will be creating the HTML
  through a WYSIWYG editor (CKEditor).
  The markup isn't guaranteed to be
  pretty or consistent.</li>
  <li>The token, <code>read_more()</code>, can
  be placed anywhere in the string,
  including being nested within a
  paragraph tag.</li>
  <li>The resulting first split string
  needs to be valid HTML for all
  reasonable uses of the token.</li>
  </ul>
  
  <p>Examples of possible uses:</p>
  
  <pre><code>&lt;p&gt;Some text here. read_more()&lt;/p&gt;
  
  &lt;p&gt;Some text read more() here.&lt;/p&gt;
  
  &lt;p&gt;read_more()&lt;/p&gt;
  
  &lt;p&gt;  read_more()&lt;/p&gt;
  
  read_more()
  </code></pre>
  
  <p>So far, I've tried just splitting the string on the token, but it leaves invalid HTML. Regex is perhaps another option. What strategy would you use to solve this and make it as bulletproof as possible? Any code snippets or hints would also be appreciated (I'm using PHP).</p>