Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Broadly speaking, it is a bad idea to try and mix HTML markup with regex. Your results will vary -- too variation much for a reliable script. If you need to parse HTML, use the HTML parser available right in PHP, DomDocument.</p> <p>To get RID of HTML is even simpler. You can use <code>strip_tags</code> to remove any and all HTML from the string, even broken markup. Your code could simply be:</p> <pre><code>$this-&gt;return_data = strip_tags(ee()-&gt;TMPL-&gt;tagdata); </code></pre> <p>Proof of concept:</p> <pre><code>$sample1 = 'mailto:email@domain.com&lt;/p&gt;'; echo 'dirty: '.htmlentities($sample1).', clean: '.htmlentities(strip_tags($sample1)); // output: dirty: mailto:email@domain.com&lt;/p&gt;, clean: mailto:email@domain.com </code></pre> <p>See it in action here: <a href="http://codepad.viper-7.com/KHsIr0" rel="nofollow">http://codepad.viper-7.com/KHsIr0</a></p> <p>One function call, no crazy regex to maintain.</p> <hr> <p>Here is an example of how to do this with DomDocument:</p> <pre><code>// create a new DomDocument object $doc = new DOMDocument(); // load the HTML into the DomDocument object (this would be your source HTML) libxml_use_internal_errors(true); $doc-&gt;loadHTML(' &lt;p&gt; &lt;br&gt; Preston Newbill&lt;br&gt; Manager&lt;br&gt; pnewbill@domain.com&lt;br&gt; &lt;a href="mailto:noob@aol.com"&gt;also email me @ noob@aol.com&lt;/a&gt;&lt;br&gt; Party 9/15/2013@10:00pm! '); libxml_clear_errors(); // grab the body, recursively check for child nodes. Turn any email addresses into links $body = $doc-&gt;getElementsByTagName('body')-&gt;item(0); checkDomNodeForEmailAddress($body); // strip off the html,head, and body $doc-&gt;removeChild($doc-&gt;firstChild); $doc-&gt;replaceChild($doc-&gt;firstChild-&gt;firstChild-&gt;firstChild, $doc-&gt;firstChild); die('&lt;hr&gt;final product:'.htmlentities($doc-&gt;saveHtml())); function checkDomNodeForEmailAddress(DOMNode $domNode) { foreach ($domNode-&gt;childNodes as $node) { if($node-&gt;hasChildNodes()) { if (strtolower($node-&gt;nodeName) != 'a') checkDomNodeForEmailAddress($node); } else { $node-&gt;nodeValue = preg_replace('/(\S+@\S+\.[^\&lt;]+)/', '&lt;a href="mailto:$1"&gt;$1&lt;/a&gt;', $node-&gt;nodeValue); } } } </code></pre> <p>Try it here: <a href="http://codepad.viper-7.com/EpdBKx" rel="nofollow">http://codepad.viper-7.com/EpdBKx</a></p> <p><strong>Documentation</strong></p> <ul> <li><code>strip_tags</code> - <a href="http://php.net/manual/en/function.strip-tags.php" rel="nofollow">http://php.net/manual/en/function.strip-tags.php</a></li> <li>DomDocument - <a href="http://php.net/manual/en/class.domdocument.php" rel="nofollow">http://php.net/manual/en/class.domdocument.php</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload