The html component provides a set functions to encode and decode html strings.
use Psl\Html;
use Psl\IO;
$name = 'Héllo </>';
IO\write_line(Html\encode($name)); // Héllo </>
IO\write_line(Html\encode_special_characters($name)); // Hello </>
-
[
Html\encode(string $html, bool $double_encoding = true, Html\Encoding $encoding = Html\Encoding::UTF_8): string
php]Convert all applicable characters to HTML entities.
- [
$double_encoding
php]: If set to false, this function will not encode existing html entities. - [
$encoding
php]: defines character set used in conversion.
use Psl; use Psl\Html; Html\encode('Héllo </>'); // Héllo </> Html\encode('Héllo </>', true); // Héllo &lt;/&gt; Html\encode('Héllo </>', false); // Héllo </>
- [
-
[
Html\decode(string $html, Html\Encoding $encoding = Html\Encoding::UTF_8): string
php]Convert all HTML entities to their applicable characters.
- [
$encoding
php]: defines character set used in conversion.
use Psl; use Psl\Html; Html\decode('Héllo </>'); // Héllo </> Html\decode('Héllo &lt;/&gt;'); // Héllo </>
- [
-
[
Html\encode_special_characters(string $html, bool $double_encoding = true, Html\Encoding $encoding = Html\Encoding::UTF_8): string
php]Convert special characters to HTML entities.
- [
$double_encoding
php]: If set to false, this function will not encode existing html entities. - [
$encoding
php]: defines character set used in conversion.
use Psl; use Psl\Html; Html\encode_special_characters('Héllo </>'); // Héllo </> Html\encode_special_characters('Héllo </>', true); // Héllo &lt;/&gt; Html\encode_special_characters('Héllo </>', false); // Héllo </>
- [
-
[
Html\decode_special_characters(string $html): string
php]Convert special characters to HTML entities.
- [
$html
php]: The string to decode.
use Psl; use Psl\Html; Html\decode_special_characters('Héllo </>'); // Héllo </> Html\decode_special_characters('Héllo &lt;/&gt;'); // Héllo </> Html\decode_special_characters('Héllo </>'); // Héllo </>
- [
-
[
Html\strip_tags(string $html, list<string> $allowed_tags = []): string
php]Strip HTML tags from a string.
- [
$html
php]: The string to strip. - [
$allowed_tags
php]: A list of tags to allow.
use Psl; use Psl\Html; Html\strip_tags('<p>Hello</p>'); // Hello Html\strip_tags('<p>Hello</p>', ['p']); // <p>Hello</p> Html\strip_tags('<p>Hello</p>', ['p', 'div']); // <p>Hello</p> Html\strip_tags('<p>Hello</p>', ['div']); // Hello
- [
-
[
enum Html\Encoding
php]defines character set used in conversion.