title: preg_match pattern to find the contents of a string between <html> and </html> tags
body: <p>I'm working on a PHP script that reads the content of emails, and pulls out certain information to store in a database.</p>
  
  <p>Using imap_fetchbody ($imap_stream, $msg_number, 1), I'm able to get at the body of the email. In some cases (especially email sent as SMS from mobile phones), the body of the email looks like this:</p>
  
  <pre><code>===------=_Part_110734_170079945.1283532109852
  Content-Type: text/html;charset=UTF-8;
  Content-Transfer-Encoding: 7bit
  Content-Disposition: inline
  
  &lt;html&gt; 
      &lt;head&gt; 
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; 
          &lt;title&gt;Multimedia Message&lt;/title&gt; 
      &lt;/head&gt; 
      &lt;body leftmargin="0" topmargin="0"&gt; 
  
  
                  &lt;tr height="15" style="border-top: 1px solid #0F7BBC;"&gt; 
                      &lt;td&gt; 
                          SMS to email test
                      &lt;/td&gt; 
                  &lt;/tr&gt; 
  
  
       &lt;/body&gt; 
  &lt;/html&gt; 
  
  
  ------=_Part_110734_170079945.1283532109852--===
  </code></pre>
  
  <p>I want to pull out the "content" of the email. So, my plan is this:</p>
  
  <p>Check to see if the body contains the "html" tags. If not, I can read it normally (it's not an HTML email).</p>
  
  <p>If it does, extract the content between the "html" tags. Then, eliminate all the other HTML tags, and the "content" is what's left.</p>
  
  <p>However, I'm pretty clueless when it comes to regex patterns.</p>
  
  <p>I tried this:</p>
  
  <pre><code>$pattern = '/&lt;html[^&gt;]*&gt;(.*?)&lt;\/html&gt;/i';
  preg_match($pattern, $body, $matches);
  // my 'content' should be in $matches[1]
  </code></pre>
  
  <p>But that didn't work (probably because $body contains newlines and other whitespace). So then I tried this:</p>
  
  <pre><code>$pattern = '/&lt;html[^&gt;]*&gt;([.\s]*?)&lt;\/html&gt;/i';
  preg_match($pattern, $body, $matches);
  </code></pre>
  
  <p>But that didn't work either.</p>
  
  <p>So, what $pattern can I use to extract all the text between the "html" tags?</p>
  
  <p><strong>UPDATE:</strong> I've stumbled into a workaround - strip all the whitespace first:</p>
  
  <pre><code>$body = preg_replace('/\s\s+/', ' ', $body);
  $pattern = '/&lt;body[^&gt;]*&gt;(.*?)&lt;\/body&gt;/';
  </code></pre>
  
  <p>I suspect this isn't the fastest or most efficient method, but it works, and is the best I've got so far. I'd still appreciate a better solution if there is one, though.</p>
  
  <p><strong>UPDATE 2:</strong> Thanks to Gumbo suggestions, I've tried a little harder to dig through the structure of the email to find the part I was looking for, instead of attempting to regex HTML. I finally found this: <a href="http://docstore.mik.ua/orelly/webprog/pcook/ch17_04.htm" rel="nofollow noreferrer">http://docstore.mik.ua/orelly/webprog/pcook/ch17_04.htm</a>, which explains how to do exactly what I needed. </p>