SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

HTML tags to print on page by htmlspecialchars function

We can display some special chars especially html tags on the screen by using htmlspecialchars function of PHP. This is required when we have to show some sample codes on the page or screen. For example I want to display this line

if ($i < 5 )


Here we can't write < as it is , in this place I have to write &lt; then while displaying this will display < on the screen. Same way some other chars are there which are to be written in different way or special care to be taken for displaying them. All these jobs can be done by using htmlspecialchars function.

This function takes care of &, < ( less than ), > ( greater than ), " double quote ( if ENT_NOQUOTES is not set. ) and single quote (only when ENT_QUOTES is set). Single quote became &#039; and double quote became &quot;. Same way < ( less than ) became &lt; and > ( greater than ) became &gt;

Now let us try with an example.

Read this line below, we want to display the formatting of this line.

Hello this is bold This is italic This is normal this is a double quote " this is & this is less than < this is greater than >

The html part of the above line is here

<b>Hello this is bold</b> <i>This is italic</i> This is normal this is a double quote " this is & this is less than < this is greater than >


Now to display the code for the above line formatted in different style we have to use htmlspecialchars funciton like this



$contents="<b>Hello this is bold</b> <i>This is italic</i> This is normal this is a double quote " this is & this is less than < this is greater than >";
echo htmlspecialchars($content);


Read how htmlspecialchar is used to generate html code for web pages

Discuss this tutorial at forum


PHP String Functions
All String Functions in PHP
str_replace: How to replace a part of a string with another string
str_ireplace: Case in-sensitive search and replace using array of strings
strlen: How to find length of a string in PHP?
trim: Removing empty space from both sides of a string
strrev: Reversing a string by using strrev function in PHP
Adding two or more strings in PHP
stristr: Searching for a presence of a string inside another string
nl2br: Adding Line breaks inside a string in place of carriage returns
split: Breaking a string to form array using delimiters
substr: Collecting part of a string
substr_count: Counting occurrence of sub string in main string
str_repeat: Repeating a string number of times
strtolower(): function to change alphabets to Lower case
strtoupper(): function to change characters to upper case letters
function to collect part of a string with two landmarks
Random string generator with number and alphabets for password
strcasecmp(): Case insensitive string comparison
strcmp(): Case sensitive string comparison
str_pad(): String pad with specified length
md5 hash of a string: encryption of a string
Separating domain and userid part from an email address using split
htmlspecialchars: Printing html special chars to the page
strip_tags: Removing html tags within a string
ucwords: Removing html tags within a string



 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
String Functions
htmlspecialchars
md5 hash
nl2br
Random string
strip_tags
str_replace
str_ireplace
strlen
strrev
stristr
split
substr
substr_count
str_repeat
strtolower
strcasecmp
strcmp
str_pad
trim
ucwords
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.