Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set ENT_QUOTES flag by default for htmlentities() function in php

I am using htmlentities($data, ENT_QUOTES) on any data fetched from database before displaying it.

Is there a way I can set the flag ENT_QUOTES by default for htmlentities() function, so that even if I write htmlentities($data) it should work as htmlentities($data, ENT_QUOTES).

As written in the documentation of php the default is ENT_COMPAT | ENT_HTML401.

For your information I am using codeigniter framework, php5.

UPDATE 1: wrapping with a custom function as suggested by Michael could help but I have already used this everywhere in the website without ENT_QUOTES flag and was wondering if there is a way provided by php to change defaults for its functions.

UPDATE 2: I think html_escape() inbuilt function provided by codeigniter (suggested by Wesley) is the best for me so that i don't have to write my own wrapper function.

like image 771
Ankit Prasad Avatar asked Nov 04 '12 23:11

Ankit Prasad


People also ask

What is the use of HTML entities () function in PHP?

htmlentities() Function: The htmlentities() function is an inbuilt function in PHP that is used to transform all characters which are applicable to HTML entities. This function converts all characters that are applicable to HTML entities.

What is the difference between HTML entities and Htmlspecialchars in PHP?

The only difference between htmlspecialchars() and htmlentities() function is that htmlspecialchars() function converts the special characters to HTML entities, whereas htmlentities() function converts all the applicable characters to html entities.

When should I use Htmlspecialchars?

You use htmlspecialchars EVERY time you output content within HTML, so it is interpreted as content and not HTML. If you allow content to be treated as HTML, you have just opened the door to bugs at a minimum, and total XSS hacks at worst.


1 Answers

There's no way to change the default flags that I know of, but the advice given to you in the comments is absolutely the best way to approach this anyways: use a wrapper function.

Conveniently, Codeigniter has one built in already, appropriately named:

echo html_escape($string);

You can pass in arrays as well, here's what it does:

/**
* Returns HTML escaped variable
*
* @access   public
* @param    mixed
* @return   mixed
*/
if ( ! function_exists('html_escape'))
{
    function html_escape($var)
    {
        if (is_array($var))
        {
            return array_map('html_escape', $var);
        }
        else
        {
            return htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
        }
    }
}

Just do a search for htmlentities in your project and replace (carefully) with html_escape. This will also provide the opportunity for you to easily make changes in the future because you can alter the function. It's a little bit of an initial time investment but well worth it.

like image 95
Wesley Murch Avatar answered Sep 27 '22 19:09

Wesley Murch