659

Online HTML Escape Tool (htmlspecialchars, htmlentities)

This tool will take your text and convert all the special characters to their proper HTML codes, so you can paste text with special characters or HTML code onto your website. It has been carefully designed so that the HTML produced by this tool looks and behaves exactly like the original text does in a text editor.

Enter Your Text:


     Tab width:      Replace line endings with <br />

How it works

Escaping text is a bit tricky when you want it to look exactly the same in HTML as it would look in a text editor. The code this page uses to escape text is presented below. If you read the comments, you'll see that the solution is not as obvious as you might think!

 1 <?php
 2 class HtmlEscape
 3 {
 4     public static function escapeText($text, $brTags, $tabWidth)
 5     {
 6         // Replace tabs with spaces -- Must be done before htmlspecialchars 
 7         // because the tab width is dependant upon the cursor position.
 8         $esc = self::tabsToSpaces($text, $tabWidth);
 9 
10         // Escape all characters that have a special meaning in HTML
11         $esc = htmlspecialchars($esc, ENT_QUOTES);
12 
13         // Replace repeated spaces with &nbsp;
14         //      This is tricky. Spaces cannot simply be replaced with &nbsp;
15         //      because the line of text will not break, so we have to leave
16         //      normal spaces in between pairs of &nbsp; to let the line break.
17         //      The space must come before the &nbsp; because we want three
18         //      spaces in a row to turn into " &nbsp; " not "&nbsp;  " (which
19         //      will look like two spaces in the browser).
20         $esc = str_replace("  ", " &nbsp;", $esc);
21 
22         // HTML ignores leading spaces in elements like <p> and <div> so we 
23         // have to replace spaces at the beginning of the line with an &nbsp;
24         // 0x20 = ASCII SPACE
25         $esc = preg_replace('/^\x20/m', "&nbsp;", $esc);
26         // The same thing happens when the space is at the end of a line.
27         // Trailing spaces matter when someone copies text from the page.
28         $esc = preg_replace('/\x20(?=\r|\n)/m', "&nbsp;", $esc);
29 
30         if($brTags)
31         {
32             // To add <br /> tags, we first normalize the line endings to \n
33             // First convert Windows-style CRLF lines to \n
34             $esc = str_replace("\r\n", "\n", $esc);
35             // Then convert Mac-style CR lines to \n. Order matters here.
36             // If we did this before replacing \r\n, we would replace \r\n with
37             // \n\n, which will be two lines instead of one.
38             $esc = str_replace("\r", "\n", $esc);
39             // Then add a <br /> before each \n
40             $esc = str_replace("\n", "<br />\n", $esc);
41         }
42 
43         return $esc;
44     }
45 
46     private static function tabsToSpaces($text, $tabWidth)
47     {
48         $spaces = "";
49         $cursor = 0;
50         for($i = 0; $i < strlen($text); $i++)
51         {
52             if($text[$i] == "\t")
53             {
54                 // Add spaces until the cursor position is divisible by 
55                 // $tabWidth, adding at least one space so that if $cursor
56                 // is already divisible by $tabWidth, we add $tabWidth spaces.
57                 $spaces .= " ";
58                 $cursor++;
59                 while($cursor % $tabWidth != 0)
60                 {
61                     $spaces .= " ";
62                     $cursor++;
63                 }
64             }
65             else
66             {
67                 $spaces .= $text[$i];
68                 $cursor++;
69                 // Reset the cursor position to zero on CR or LF
70                 if($text[$i] == "\n" || $text[$i] == "\r")
71                     $cursor = 0;
72             }
73         }
74         return $spaces;
75     }
76 }
77 ?>