Print special characters in php
After much banging-head-on-table, I have a bit better understanding of the issue that I wanted to post for anyone else who may have had this issue. Show While the UTF-8 character set will display special characters on the client, the server, on the other hand, may not be so accomodating and would print special characters such as To make sure your server will print them correctly, use the
This will print correctly: Edit (4 years later):I have a little better understanding now. The reason this works is that the client (browser) is being told, through the response ❮ PHP String Reference ExampleConvert the predefined characters "<" (less than) and ">" (greater than) to HTML entities: $str = "This is some bold text."; The HTML output of the code above will be (View Source):
This is some <b>bold</b> text. The browser output of the code above will be: This is some bold text. Try it Yourself » Definition and UsageThe htmlspecialchars() function converts some predefined characters to HTML entities. The predefined characters are:
Tip: To convert special HTML entities back to characters, use the htmlspecialchars_decode() function. Syntaxhtmlspecialchars(string,flags,character-set,double_encode) Parameter Values
Technical Details
More ExamplesExampleConvert some predefined characters to HTML entities: $str = "Jane & 'Tarzan'"; The HTML output of the code above will be (View Source):
Jane & 'Tarzan' Jane & 'Tarzan' Jane & 'Tarzan' The browser output of the code above will be: Jane & 'Tarzan' Try it Yourself » ExampleConvert double quotes to HTML entities: $str = 'I love "PHP".'; The HTML output of the code above will be (View Source):
I love "PHP". The browser output of the code above will be: I love "PHP". Try it Yourself » ❮ PHP String Reference How do I allow special characters in PHP?Tip: To convert special HTML entities back to characters, use the htmlspecialchars_decode() function.. & (ampersand) becomes &. " (double quote) becomes ". ' (single quote) becomes '. < (less than) becomes <. > (greater than) becomes >. What does Htmlspecialchars mean in PHP?Description. The htmlspecialchars() function is used to converts special characters ( e.g. & (ampersand), " (double quote), ' (single quote), < (less than), > (greater than)) to HTML entities ( i.e. & (ampersand) becomes &, ' (single quote) becomes ', < (less than) becomes < (greater than) becomes > ).
What is the difference between Htmlentities and Htmlspecialchars in PHP?Difference between htmlentities() and htmlspecialchars() function: The only difference between these function is that htmlspecialchars() function convert the special characters to HTML entities whereas htmlentities() function convert all applicable characters to HTML entities.
Can UTF 8 handle special characters?UTF-8 represents ASCII invariant characters a-z, A-Z, 0-9, and certain special characters such as ' @ , . + - = / * ( ) the same way that they are represented in ASCII.
|