Last active
September 26, 2023 09:54
-
-
Save rizkytegar/7388e5a117cd8a7890b83d1e67cfa3bd to your computer and use it in GitHub Desktop.
Laravel XSS Filtering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Escapes special characters in a string for use in HTML. | |
* | |
* @param mixed $value The value to be escaped. | |
* @param bool $doubleEncode Indicates if existing entities should be encoded or not. Default is true. | |
* @return string The escaped string. | |
*/ | |
function e($value, $doubleEncode = true) | |
{ | |
if ($value instanceof DeferringDisplayableValue) { | |
$value = $value->resolveDisplayableValue(); | |
} | |
if ($value instanceof Htmlable) { | |
return $value->toHtml(); | |
} | |
if ($value instanceof BackedEnum) { | |
$value = $value->value; | |
} | |
return htmlspecialchars($value ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', $doubleEncode); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment