Title: How to directly call the laravel sanitize function?
Tags: laravel
Question: In a blade file one can do this:
{{ $someVariable }}
This sanitizes $someVariable as opposed to calling it like this:
{!! $someVariable  !!}
What PHP function is called for the first case? Is there a way to do this outside of a blade file?
Comment: https://laravel.com/docs/5.5/blade#displaying-data "Blade {{ }} statements are automatically sent through PHP's `htmlspecialchars` function to prevent XSS attacks."

Here is the accepted answer: The function that ends up being called is e, for 'escape'.
&quot;Encode HTML special characters in a string.&quot;
{{ ... }} is replaced with <?php echo e(...); ?&gt;.
It is defined in vendor/laravel/framework/src/Illuminate/Support/helpers.php. It calls htmlspecialchars but also handles special objects that are Htmlable or DeferringDisplayableValue.
&quot;The e function runs PHP's htmlspecialchars function with the double_encode option set to true by default&quot; - Laravel 9.x Docs - Helpers - String Helpers - e
On a side note, this is not sanitizing, it is just escaping.
Comment for this answer: What would you rate is the difference between sanitizing and escaping? I just want it to avoid posting malicious code?
Comment for this answer: @coderama sanitizing is removing data, like the html tags or javascript from what you have and saving it with that removed ... escaping is just encoding it so the browser doesn't see it as markup

Here is another answer: According to the Laravel documentation you can do it with htmlspecialchars()
Example:
$new = htmlspecialchars(&quot;<a href='test'&gt;Test</a&gt;&quot;, ENT_QUOTES);
echo $new; // &amp;lt;a href=&amp;#039;test&amp;#039;&amp;gt;Test&amp;lt;/a&amp;gt;
https://www.php.net/manual/en/function.htmlspecialchars.php

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:06/05/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<laravel>', 'Title': 'How to directly call the laravel sanitize function?', 'ViewCount': 50, 'id': 72508782, 'user_AboutMe': '', 'user_AccountId': 49185.0, 'user_DisplayName': 'rockstardev', 'user_Reputation': 13413.0, 'user_Views': 2625.0, 'user_id': 146366.0}}