Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let me begin by saying that, in my opinion, what you are trying to do is fundementally wrong. What if someone wants to type a less-than sign? Personally, I see <code>htmlentities()</code> as a way to make sure users <em>can't</em> enter their own HTML code.</p> <p>If you need users to be able to style text, there are many solutions already made for that (check out <a href="http://tinymce.moxiecode.com/" rel="nofollow">TinyMCE</a> or <a href="http://daringfireball.net/projects/markdown/basics" rel="nofollow">Markdown</a>, for example).</p> <p>If you must allow users to input HTML tags, <em>and</em> you must assume they don't know how to use entities, here is a simple function that works:</p> <pre><code>function my_htmlentities ($str) { // We'll append everything to this. $result = ''; // Continue while there are HTML tags. while (($lt = strpos($str, '&lt;')) !== false) { // Run `htmlentities` on everything before the tag, and pop it // off the original string. $result .= htmlentities(substr($str, 0, $lt)); $str = substr($str, $lt); // We want to continue until we reach the end of the tag. I know // these loops are bad form. Sorry. I still think in F77 :p while (true) { // Find the closing tag as well as quotes. $gt = strpos($str, '&gt;'); $quot = strpos($str, '"'); // If there is no closing bracket, append the rest of the tag // as plaintext and exit. if ($gt === false) return $result . $str; // If there is a quote before the closing bracket, take care // of it. if ($quot !== false &amp;&amp; $quot &lt; $gt) { // Grab everything before the quote. $result .= substr($str, 0, $quot+1); $str = substr($str, $quot+1); // Find the closing quote (if there is none, append and // exit). if (($quot = strpos($str, '"')) === false) return $result . $str; // Grab the inside of the quote. $result .= substr($str, 0, $quot+1); $str = substr($str, $quot+1); // Start over as if we were at the beginning of the tag. continue; } // We just have the closing bracket to deal with. Deal. $result .= substr($str, 0, $gt+1); $str = substr($str, $gt+1); break; } } // There are no more tags, so we can run `htmlentities()` on the // rest of the string. return $result . htmlentities($str); // Alternatively, if you want users to be able to enter their own // entities as well, you'll have to use this last line instead: return str_replace('&amp;amp;', '&amp;', $result . htmlentities($str)); } </code></pre> <p>But please let me reiterate: this is extremely insecure! I'll give you the benefit of the doubt that you know what you want, but I don't think you (or anyone) should want this.</p>
    singulars
    1. This table or related slice is empty.
    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. 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