3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Sanitizer { public function sanitizeSingle(&$string) { if (function_exists("get_magic_quotes_gpc") && get_magic_quotes_gpc()) { $string = stripslashes($string); } $string = trim(htmlspecialchars($string, ENT_QUOTES)); } public function sanitize($data) { if (is_array($data)) { array_walk_recursive($data, [$this, 'sanitizeSingle']); } else { $this->sanitizeSingle($data); } return $data; } } $array = ['one' => ['a ', ' b ', ' c'], 'two' => " <a href='test'>Test</a>"]; $string = ' another "test"'; $sanitizer = new Sanitizer(); var_export($sanitizer->sanitize($array)); echo "\n---\n"; var_export($sanitizer->sanitize($string));
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.26, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
array ( 'one' => array ( 0 => 'a', 1 => 'b', 2 => 'c', ), 'two' => '&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;', ) --- 'another &quot;test&quot;'
Output for 7.4.0 - 7.4.25, 7.4.27 - 7.4.33
Deprecated: Function get_magic_quotes_gpc() is deprecated in /in/7drYO on line 5 Deprecated: Function get_magic_quotes_gpc() is deprecated in /in/7drYO on line 5 Deprecated: Function get_magic_quotes_gpc() is deprecated in /in/7drYO on line 5 Deprecated: Function get_magic_quotes_gpc() is deprecated in /in/7drYO on line 5 array ( 'one' => array ( 0 => 'a', 1 => 'b', 2 => 'c', ), 'two' => '&lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;', ) --- Deprecated: Function get_magic_quotes_gpc() is deprecated in /in/7drYO on line 5 'another &quot;test&quot;'

preferences:
137.21 ms | 409 KiB | 5 Q