<?php
$str = '<p>\' ‘ ’ ‚ " “ ” „ &amp; © ® ™ ° </p>';
echo clean($str);
echo PHP_EOL.PHP_EOL;
$htmlent = htmlentities($str, ENT_HTML5 | ENT_QUOTES, 'UTF-8');
echo $htmlent;
echo PHP_EOL.PHP_EOL;
echo clean($htmlent);
function clean($str_clean, $strict_clean = true, $punct_clean = true) {
$base_pool = array(
'‘' => '‘',
'’' => '’',
'‚' => '‚',
'“' => '“',
'”' => '”',
'„' => '„',
'©' => '©',
'®' => '®',
'™' => '™',
'°' => '°',
'•' => '•',
'–' => '–',
'—' => '—',
'…' => '…',
'¼' => '¼',
'½' => '½',
'¾' => '¾',
'‰' => '‰',
'±' => '±',
'†' => '†',
'‡' => '‡',
'&amp;' => '&',
' ' => ' '
);
$strict_pool = array(
'\'' => ''',
'"' => '"',
"\r" => ' ',
"\n" => ' ',
"\t" => ' '
);
// if strict param is true we will clean harder (default true)
if ($strict_clean) {
$base_pool = array_merge($strict_pool, $base_pool);
}
foreach ($base_pool as $key => $value) {
$str_clean = str_replace($key, $value, $str_clean); // iterate through pool key => values and replace in $str_clean
}
// if punct param is true we will add spaces after punctuation (default true)
if ($punct_clean) {
$str_clean = preg_replace('/([!?,.])(\S)/', '$1 $2', $str_clean); // parse punctuation and add trail space
}
$str_clean = preg_replace("/\s+/", ' ', $str_clean); // filter multiple spaces into single
return $str_clean;
}
- Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- <p>' ‘ ’ ‚ " “ ” „ & © ® ™ ° </p>
<p>' ‘ ’ ‚ " “ ” „ &amp;amp; © ® ™ ° &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
<p>' ‘ ’ ‚ " “ ” „ &amp;amp; © ® ™ ° &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
<p>' ‘ ’ ‚ " “ ” „ & © ® ™ ° </p>
<p>' ‘ ’ ‚ " “ ” „ &amp;amp; © ® ™ ° &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
<p>' ‘ ’ ‚ " “ ” „ &amp;amp; © ® ™ ° &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
preferences:
249.72 ms | 403 KiB | 330 Q