|
użytkowników online: 77
|
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
|
|
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
Descriptionstring 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 Name | Description |
|---|
| 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. |
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 | Charset | Aliases | Description |
|---|
| ISO-8859-1 | ISO8859-1 |
Western European, Latin-1
| | ISO-8859-15 | ISO8859-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.
| | cp866 | ibm866, 866 |
DOS-specific Cyrillic charset.
This charset is supported in 4.3.2.
| | cp1251 | Windows-1251, win-1251, 1251 |
Windows-specific Cyrillic charset.
This charset is supported in 4.3.2.
| | cp1252 | Windows-1252, 1252 |
Windows specific charset for Western European.
| | KOI8-R | koi8-ru, koi8r |
Russian. This charset is supported in 4.3.2.
| | BIG5 | 950 |
Traditional Chinese, mainly used in Taiwan.
| | GB2312 | 936 |
Simplified Chinese, national standard character set.
| | BIG5-HKSCS | |
Big5 with Hong Kong extensions, Traditional Chinese.
| | Shift_JIS | SJIS, 932 |
Japanese
| | EUC-JP | EUCJP |
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>";
echo htmlentities($str);
echo htmlentities($str, ENT_QUOTES);
?>
|
|
See also html_entity_decode(),
get_html_translation_table(),
htmlspecialchars(), nl2br(),
and urlencode().
User Contributed Notestimburgan 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 +).
chars_decode() will decode a string encoded by chars_encode().
<?php
function chars_encode($string, $encodeAll = false)
{
$chars = array();
$ent = null;
$chars = preg_split("//", $string, -1, PREG_SPLIT_NO_EMPTY);
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);
}
function chars_decode($string)
{
$tok = 0;
$cur = 0;
$chars = null;
while ( $cur < strlen($string) )
{
$tok = strpos($string, "&#", $cur);
if ( $tok === false )
$tok = strlen($string);
if ( preg_match("/^(\w| )$/",substr($string, $cur, 1)) )
{
$chars .= substr($string, $cur, $tok - $cur);
}
else
{
$cur += 2;
$tok = strpos($string, ';', $cur);
$chars .= chr(substr($string, $cur, $tok - $cur));
$tok++;
}
$cur = $tok;
}
return $chars;
}
$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('‘',
chr(96),
'’',
'„',
'”',
'“',
'…',
'–');
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 "", 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($_) {
$_ = str_replace(array('–', '—'),
array('-', '--'), $_);
$_ = str_replace(array('‘', '’'), "'", $_);
$_ = str_replace(array('“', '”'), '"', $_);
$_ = str_replace('…', '...', $_); 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 (
|