Η συνάρτηση PHP htmlentities()
Παράδειγμα
Convert characters to HTML entities:
<?php $str = "<? W3S?h????>"; echo htmlentities($str); ?>
The HTML output of the above code is as follows (view source code):
<!DOCTYPE html> <html> <body> <© W3Sçh°°¦§> </body> </html>
Browser output of the above code:
<? W3S?h????>
Δефίνιση και χρήση
Η συνάρτηση htmlentities() μετατρέπει χαρακτήρες σε HTML entities.
Συμβουλή:Για να μετατρέψετε HTML entities σε χαρακτήρες, χρησιμοποιήστε τη συνάρτηση html_entity_decode() Η συνάρτηση.
Συμβουλή:Χρησιμοποιήστε get_html_translation_table() Η συνάρτηση για επιστροφή της μεταφραστικής λέξεως που χρησιμοποιεί η function htmlentities().
Γραμματική
htmlentities(string,flags,character-set,double_encode)
Παράμετροι | Περιγραφή |
---|---|
string | Απαιτείται. Καθορίζει την αλφαβητική ακολουθία που θα μετατραπεί. |
flags |
Επιλογή. Καθορίζει πώς θα χειριστούν τα εισαγωγικά, τις μη έγκυρες κωδικοποιήσεις και ποιον τύπο εγγράφου θα χρησιμοποιηθεί. Διαθέσιμες τύποι εισαγωγικών:
Μη έγκυρες κωδικοποιήσεις:
Προσθέτητες σημαίες για τον καθορισμό του τύπου εγγράφου που χρησιμοποιείται:
|
character-set |
Επιλογή. Μια αλφαβητική ακολουθία που καθορίζει τον χαρακτήρα συνδυασμό που θα χρησιμοποιηθεί. Allowed values:
Note:In versions of PHP before 5.4, unrecognized character sets will be ignored and replaced by ISO-8859-1. Starting from PHP 5.4, unrecognized character sets will be ignored and replaced by UTF-8. |
double_encode |
Optional. Boolean value, specifies whether to encode existing HTML entities.
|
Technical details
Return value: |
Return the converted string. If string If it contains invalid encoding, it will return an empty string unless ENT_IGNORE or ENT_SUBSTITUTE flags are set. |
PHP version: | 4+ |
Update log: |
In PHP 5,character-set The default value of the parameter has been changed to UTF-8. In PHP 5.4, new features were added: ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_HTML5, ENT_XML1 and ENT_XHTML. In PHP 5.3, a new feature was added: ENT_IGNORE. In PHP 5.2.3, a new feature was added: double_encode Parameters. In PHP 4.1, a new feature was added: character-set Parameters. |
More examples
Example 1
Convert characters to HTML entities:
<?php $str = "Bill & 'Steve'"; echo htmlentites($str, ENT_COMPAT); // Convert double quotes only echo "<br>"; echo htmlentites($str, ENT_QUOTES); // Convert double quotes and single quotes echo "<br>"; echo htmlentities($str, ENT_NOQUOTES); // Do not convert any quotes ?>
The HTML output of the above code is as follows (view source code):
<!DOCTYPE html> <html> <body> Bill & 'Steve'<br> Bill & 'Tarzan'<br> Bill & 'Steve' </body> </html>
Browser output of the above code:
Bill & 'Steve' Bill & 'Steve' Bill & 'Steve'
Example 2
Convert some characters to HTML entities using the Western European character set:
<?php $str = "My name is Øyvind Øsane. I'm Norwegian."; echo htmlentities($str, ENT_QUOTES, "ISO-8859-1"); // Will only convert double quotes (not single quotes), and uses the character-set Western European ?>
The HTML output of the above code is as follows (view source code):
<!DOCTYPE html> <html> <body> My name is Øyvind Øsane. I'm Norwegian. </body> </html>
Browser output of the above code:
My name is Øyvind Øsane. I'm Norwegian.