PHP htmlentities() Function

PHP htmlentities() function is string function, which is used to convert character to HTML entities.

Syntax:


htmlentities(string,flags,character-set,double_encode);  
  
Parameter Description Required/Optional
String Specify the string to convert. Required
flags Specify how to manage quotes, invalid encoding. optional
Character Specify the character set optional
Double_encode Specify a Boolean value. optional

Example 1


<?php  
$str = '<a href="https://www.google.com/">Go to javatpoint.com</a>';  
echo htmlentities($str);    
?>
        

Output

Go to google.com

Example 2


<?php  
$str = "Hello PHP : 'E=MC?'";  
echo htmlentities($str, ENT_COMPAT); // Will only convert double quotes  
echo "<br>";  
echo htmlentities($str, ENT_QUOTES); // Converts double and single quotes  
echo "<br>";  
echo htmlentities($str, ENT_NOQUOTES); // Does not convert any quotes     
?>
        

Output

Hello PHP : 'E=MC�' Hello PHP : 'E=MC�' Hello PHP : 'E=