Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 77
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Po wysłaniu do Dariusza problemu jeszcze nie opisanego w poradach, odpowiedź pojawia się na stronach już po 24 godzinach. To jedna z najważniejszych zalet serwisu! Za około 100 złotych rocznie mam profesjonalnego i doświadczonego konsultanta od technologii internetowych! Polecam serwis z poradami każdemu webmasterowi, niezależnie od stażu pracy i umiejętności.

Paweł Kowalski
grupa hiperMEDIA.pl

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

htmlentities

(PHP 3, PHP 4, PHP 5)

htmlentities --  Convert all applicable characters to HTML entities

Description

string htmlentities ( string string [, int quote_style [, string charset]] )

This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.

Like htmlspecialchars(), the optional second quote_style parameter lets you define what will be done with 'single' and "double" quotes. It takes on one of three constants with the default being ENT_COMPAT:

Tabela 1. Available quote_style constants

Constant NameDescription
ENT_COMPATWill convert double-quotes and leave single-quotes alone.
ENT_QUOTESWill convert both double and single quotes.
ENT_NOQUOTESWill leave both double and single quotes unconverted.

Support for the optional quote parameter was added in PHP 4.0.3.

Like htmlspecialchars(), it takes an optional third argument charset which defines character set used in conversion. Support for this argument was added in PHP 4.1.0. Presently, the ISO-8859-1 character set is used as the default.

Following character sets are supported in PHP 4.3.0 and later.

Tabela 2. Supported charsets

CharsetAliasesDescription
ISO-8859-1ISO8859-1 Western European, Latin-1
ISO-8859-15ISO8859-15 Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1(ISO-8859-1).
UTF-8  ASCII compatible multi-byte 8-bit Unicode.
cp866ibm866, 866 DOS-specific Cyrillic charset. This charset is supported in 4.3.2.
cp1251Windows-1251, win-1251, 1251 Windows-specific Cyrillic charset. This charset is supported in 4.3.2.
cp1252Windows-1252, 1252 Windows specific charset for Western European.
KOI8-Rkoi8-ru, koi8r Russian. This charset is supported in 4.3.2.
BIG5950 Traditional Chinese, mainly used in Taiwan.
GB2312936 Simplified Chinese, national standard character set.
BIG5-HKSCS  Big5 with Hong Kong extensions, Traditional Chinese.
Shift_JISSJIS, 932 Japanese
EUC-JPEUCJP Japanese

Notatka: Any other character sets are not recognized and ISO-8859-1 will be used instead.

If you're wanting to decode instead (the reverse) you can use html_entity_decode().

Przykład 1. A htmlentities() example

<?php
$str
= "A 'quote' is <b>bold</b>";

// Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str);

// Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str, ENT_QUOTES);
?>

See also html_entity_decode(), get_html_translation_table(), htmlspecialchars(), nl2br(), and urlencode().




User Contributed Notes

timburgan at gmail dot com
02-Feb-2006 03:39

chars_encode() will, by default, convert ALL non-alpha-numeric and non-space characters in a string to it's ASCII HTML character code (i.e. + to &#43;).

chars_decode() will decode a string encoded by chars_encode().

<?php

/**
 * Encode each char in a string to its HTML ASCII code.
 * Characters encoded by this function can be decoded
 * by chars_decode().
 *
 * This function will work with PHP >= 3.0.9, 4, 5.
 *
 * @author    Tim Burgan <timburgan@gmail.com>
 * @version    1.0.0
 * @link      http://timburgan.com/
 * @link      http://php.net/manual/function.htmlentities.php
 * @param      string  $string    String to encode
 * @param      bool    $encodeAll Optional. Default is false. If true all chars are encoded, if false, only non-alpha-numeric and non-space chars are encoded.
 * @return    string  Returns a string of encoded chars
 */
function chars_encode($string, $encodeAll = false)
{
  
// declare variables
  
$chars = array();
  
$ent = null;
  
  
// split string into array
  
$chars = preg_split("//", $string, -1, PREG_SPLIT_NO_EMPTY);
  
  
// encode each character
  
for ( $i = 0; $i < count($chars); $i++ )
   {
     if (
preg_match("/^(\w| )$/",$chars[$i]) && $encodeAll == false )
        
$ent[$i] = $chars[$i];
     else
        
$ent[$i] = "&#" . ord($chars[$i]) . ";";
   }
  
   if (
sizeof($ent) < 1)
     return
"";
  
   return
implode("",$ent);
}

/**
 * Decode each char in a string from its HTML ASCII code.
 * Characters denoded by this function were encoded
 * by chars_encode().
 *
 * This function will work with PHP >= 3.0.9, 4, 5.
 *
 * @author      Tim Burgan <timburgan@gmail.com>
 * @version    1.0.0
 * @link        http://timburgan.com/
 * @link        http://php.net/manual/function.html-entity-decode.php
 * @param      string  $string    String to decode
 * @return      string  Returns a string of decoded chars
 */
function chars_decode($string)
{
  
// declare variables
  
$tok = 0;
  
$cur = 0;
  
$chars = null;
  
  
// move through the string until the end is reached
  
while ( $cur < strlen($string) )
   {
    
// find the next token
    
$tok = strpos($string, "&#", $cur);
    
    
// if no more tokens exist, move pointer to end of string
    
if ( $tok === false )
        
$tok = strlen($string);
    
    
// if the current char is alpha-numeric or a space
    
if ( preg_match("/^(\w| )$/",substr($string, $cur, 1)) )
     {
        
$chars .= substr($string, $cur, $tok - $cur);
     }
    
// the current char must be the start of a token
    
else
     {
        
$cur += 2;
        
$tok = strpos($string, ';', $cur);
        
$chars .= chr(substr($string, $cur, $tok - $cur));
        
$tok++;
     }
    
    
// move the current pointer to the next token
    
$cur = $tok;
   }
  
   return
$chars;
}

/* Example usage
***********************************************/

$string = '<a href="http://timburgan.com" rel="external" title="Go to timburgan.com">Tim Burgan</a>';

$encoded = chars_encode($string);
$decoded = chars_decode($encoded);

echo <<<HEREDOCS

<h4>Original String Output</h4>
{$string}

<hr/>

<h4>Encoded String Output</h4>
{$encoded}

<hr/>

<h4>Decoded String Output</h4>
{$decoded}

HEREDOCS;

?>


Bartek
31-Jan-2006 11:06

I use this function to convert imput from MS Word into html  (ascii) compatible output. I hope it would work also for you.

I have enabled magic_quotes on my server so maybe you won't need stripslashes and addslashes.
I've also noticed that Opera 8.51 browses behaves somehow different from IE 6 and Firefox 1.5. I haven't check this functions with other browsers.

<?php
function convert_word_to_ascii($string)
{
  
$string = stripslashes($string);
  
   if (
stristr($_SERVER['HTTP_USER_AGENT'], "Opera") )
  
$search = array('&#8216;',
              
chr(96),
              
'&#8217;',
              
'&#8222;',
              
'&#8221;',
              
'&#8220;',
              
'&#8230;',
              
'&#8211;');
                          
   if (
stristr($_SERVER['HTTP_USER_AGENT'], "Firefox") || stristr($_SERVER['HTTP_USER_AGENT'], "MSIE") )
  
$search = array(chr(145),
              
chr(146),
              
chr(96),
              
chr(132),
              
chr(147),
              
chr(148),
              
chr(133),
              
chr(150));
                          
  
$replace = array(    "'",
              
"'",
              
"'",
              
'"',
              
'"',
              
'"',
              
'...',
              
'-');

  
$new_string = str_replace($search, $replace, $string);
   return
addslashes($new_string);
};
?>


24-Jan-2006 02:20

Please, don't use htmlentities to avoid XSS! Htmlspecialchars is enough!

If you don't specify the encoding, Latin1 will be used, so there is a problem if someone wants to use your software in a non-English environment.


mailing at jcn50 dot com
21-Jan-2006 07:25

Convert any language (Japanese, French, Chinese, Russian, etc...) to unicode HTML entities like &#XXXX;
In one line!

$new=mb_convert_encoding($s,"HTML-ENTITIES","auto");

where $s is your string (may be a FORM submitted one).

Enjoy~


nospam at donadio dot com dot br
19-Dec-2005 01:35

Sometimes you need to escape stuff for JavaScript, not HTML. I just modified the function posted by webmaster at swirldrop dot com. The difference is that while htmlencoded strings are something like "&#20;", JavaScript requires something like "\x20". The function below cuts it;

<?php

function js_escape_string($str){
  return
preg_replace('/[^!-%\x27-;=?-~ ]/e', '"\\x".ord("$0")', $str);
}

?>

Also, note some problems with people copying and pasting from MS Word and other word processors. They usually come with "strange" characters, like "curly" quotes (a.k.a. "typographer's quotes", "educated quotes" and "smart quotes"). These won't be encoded by the function above, nor will they be escaped by addslashes, for example.

In my case, I wanted to straighten all the quotes. Check the function below, extracted from SmartyPants <http://daringfireball.net>. It does it, and comes wih some other goodies):

<?php

function StupefyEntities($_) {
#
#  Parameter:  String.
#  Returns:    The string, with each SmartyPants HTML entity translated to
#              its ASCII counterpart.
#
#  Example input:  &#8220;Hello &#8212; world.&#8221;
#  Example output: "Hello -- world."
#

                       #  en-dash    em-dash
  
$_ = str_replace(array('&#8211;', '&#8212;'),
                     array(
'-',      '--'), $_);

  
# single quote        open      close
  
$_ = str_replace(array('&#8216;', '&#8217;'), "'", $_);

  
# double quote        open      close
  
$_ = str_replace(array('&#8220;', '&#8221;'), '"', $_);

  
$_ = str_replace('&#8230;', '...', $_); # ellipsis

  
return $_;
}

?>


edo at edwaa dot com
18-Nov-2005 05:48

A version of the xml entities function below. This one replaces the "prime" character (

 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt