• home
  • forum
  • my
  • kt
  • download
  • Escape HTML Characters

    Author: 2008-08-26 10:17:03 From:

    It is present in PHP4 and PHP5. In the simplest form, it takes as parameter the string to be converted:

    $str='<a href="http://www.reconn.us">Reconn.us</a>'
    $escaped_html=htmlentities($str);

    The result will be like this:

    &lt;a href="http://www.reconn.us"&gt;Reconn.us&lt;/a&gt;

    The text is converted so every element that has a corespondent in HTML will be converted to that. For example,  '&' (ampersand) becomes '&'.

     If you also want to convert quotes, you may want to make use of the second parameter of this function, quote_style :

    $str='<a href="http://www.reconn.us">Reconn.us</a>'
    $escaped_html=htmlentities($str,ENT_QUOTES);

     This will result in this string:

    &lt;a href=&quot;http://www.reconn.us&quot;&gt;Reconn.us&lt;/a&gt;

     The options for quote_style parameter are:

         - ENT_COMPAT - Will convert double-quotes and leave single-quotes alone.

         -ENT_QUOTES - Will convert both double and single quotes.

         - ENT_NOQUOTES - Will leave both double and single quotes unconverted. (This is the default)

     The third parameter for this function is charset and the default is ISO-8859-1.

     To see how this function works, try our Online Escape HTML Tool !

    Is very much like htmlentities() function. Unlike this function, it does not convert all applicable characters to HTML entities, but only some of them:

        - '&' (ampersand) becomes '&'

        - '"' (double quote) becomes '"' when ENT_NOQUOTES is not set.

        - ''' (single quote) becomes '&#039;' only when ENT_QUOTES is set.

        - '<' (less than) becomes '<'

        - '>' (greater than) becomes '>'

     Otherwise, functions parameters are the same: string htmlspecialchars ( string $string [, int $quote_style [, string $charset]] );

    To see the difference between these two functions I will show you an example:

     The simple text:

    <b>Confirmar contraseña</b>

    for htmlentities() function will output:

     &lt;b&gt;Confirmar contrase&ntilde;a&lt;/b&gt;

    and for htmlspecialchars() function will output:

     &lt;b&gt;Confirmar contraseña&lt;/b&gt;

    See the difference? For the first function, the special Spanish character ñ is translated also, while for the second is left unchanged.

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (7)
      Discussion Boards (8)
      E Commerce (8)
      Email Systems (13)
      Error Handling (7)
      File Manipulation (24)
      Flash and PHP (6)
      Form Processing (19)
      Guestbooks (12)
      Image Manipulation (21)
      Installing PHP (7)
      Introduction to PHP (23)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (53)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot