You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
1.1 KiB
25 lines
1.1 KiB
6 years ago
|
<?php
|
||
|
declare(strict_types=1);
|
||
|
namespace dW\HTML5;
|
||
|
|
||
|
trait EscapeString {
|
||
|
protected function escapeString(string $string, bool $attribute = false): string {
|
||
|
# Escaping a string (for the purposes of the algorithm above) consists of
|
||
|
# running the following steps:
|
||
|
|
||
|
# 1. Replace any occurrence of the "&" character by the string "&amp;".
|
||
|
# 2. Replace any occurrences of the U+00A0 NO-BREAK SPACE character by the
|
||
|
# string "&nbsp;".
|
||
|
$string = str_replace(['&', chr(0x00A0)], ['&amp;', '&nbsp;'], $string);
|
||
|
# 3. If the algorithm was invoked in the attribute mode, replace any
|
||
|
# occurrences of the """ character by the string "&quot;".
|
||
|
# 4. If the algorithm was not invoked in the attribute mode, replace any
|
||
|
# occurrences of the "<" character by the string "&lt;", and any
|
||
|
# occurrences of the ">" character by the string "&gt;".
|
||
|
if ($attribute) {
|
||
|
$string = str_replace(['"', '<', '>'], ['&quot;', '&lt;', '&gt;'], $string);
|
||
|
}
|
||
|
|
||
|
return $string;
|
||
|
}
|
||
|
}
|