Ec (2); & nbsp; copy the code functionihtmlspecialchars ($ string) {& nbsp; if (is_array ($ string) & nbsp ;{& nbsp; & nbsp; foreach ($ stringas $ key & gt; $ val) & nbsp; {& nbsp; script ec (2); script
| The Code is as follows: |
|
Function ihtmlspecialchars ($ string)
{
If (is_array ($ string ))
{
Foreach ($ string as $ key => $ val)
{
$ String [$ key] = ihtmlspecialchars ($ val );
}
} Else
{
$ String = preg_replace ('/& (# (d {3, 5} | x [a-fa-f0-9] {4 }) | [a-za-z] [a-z0-9] {2, 5});)/',' & \ 1 ',
Str_replace (array ('&', '"', '<', '>'), array ('&', '"', '<', '> '), $ string ));
}
Return $ string;
} |
// Instance
| The Code is as follows: |
|
|
$ Str = 'dfdfs ';
Echo ihtmlspecialchars ($ str );
Echo'
';
// Echo <a href = "www.111cn.net"> dfdfd </a> to prevent some unsafe factors.
Echo htmlspecialchars ($ str );
// Print <a href = "www.111cn.net"> dfdfd </a> |
/*
The two results are identical, so I personally think that the built-in functions of php are more efficient than user-defined functions.
*/
?>