php mysql retain \ in data typed
Posted: Fri Jun 03, 2011 12:21 pm
hi everyone.
i'm trying to figure out how to retain slashes typed in form fields which are saved into a mysql database.
for e.g. if \\ is typed, \\ is saved. if \ is typed, \ is saved.
i'm stumped. any helpful hints?
i'm trying to figure out how to retain slashes typed in form fields which are saved into a mysql database.
for e.g. if \\ is typed, \\ is saved. if \ is typed, \ is saved.
i'm stumped. any helpful hints?
Code: Select all
<?php
function safe($var){
$pattern = '/&(#)?[a-zA-Z0-9]{0,};/';
if (is_array($var)) { // If variable is an array
$out = array(); // Set output as an array
foreach ($var as $key => $v) {
$out[$key] = safe($v); // Run formspecialchars on every element of the array and return the result. Also maintains the keys.
}
} else {
$out = $var;
while (preg_match($pattern,$out) > 0) {
$out = htmlspecialchars_decode($out,ENT_QUOTES);
}
$out = htmlspecialchars(mysql_real_escape_string(trim($out)), ENT_QUOTES,'UTF-8',true);
}
return $out;
}
?>