Skip to content

Instantly share code, notes, and snippets.

@rizkytegar
Last active September 26, 2023 09:54
Show Gist options
  • Save rizkytegar/7388e5a117cd8a7890b83d1e67cfa3bd to your computer and use it in GitHub Desktop.
Save rizkytegar/7388e5a117cd8a7890b83d1e67cfa3bd to your computer and use it in GitHub Desktop.
Laravel XSS Filtering
<?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