您可以用一点创意过滤内容,以使其保持一致。在此示例中,我将使用 htmlentities 转换所有内容,但没有必要以这种方式将它们存储在数据库中。您需要确保在数据库注入之前对 PDO 使用 mysqli_real_escape_string() 或 quote() 之类的东西。
<?php
//$height = $_POST['height'];
$heights = array('5\' 6"','5ft 6in','5 feet 6 inches','5.5\'','5\'6"','5 1/2\'','3 foot 5 inches','2ft 8in','3 1/4in','3 1/4ft');
$patterns = array(
//Double Quotes
'!"!',
'!“!',
'!”!',
'!“!',
'!”!',
'!″!',
'!″!',
'!in(ch(es)?|\.)?!',
//Single Quotes
'!´!',
'!‘!',
'!&#[0]?39;!',
'!’!',
'!‘!',
'!’!',
'!′!',
'!′!',
'!f(oo|ee)?t\.?!',
//Conversions
'!( 1/2|\.5)'!',
'!( 1/4|\.25)'!',
'!( 1/3|\.3(3(3)?)?)'!',
'!( 3/4|\.75)'!',
//cleanup
'! (&)!',
'!;([0-9])!',
//fraction to decimal inch conversions
'! 1/2!','! 1/4!','! 1/3!','! 3/4!',
);
$replacements = array(
'"','"','"','"','"','"','"','"',
''',''',''',''',''',''',''',''',''',
'' 6"','' 3"','' 4"','' 9"',"$1","; $1",
'.5','.25','.33','.75',
);
echo "<pre>";
foreach($heights as $value){
$value = htmlentities($value,ENT_QUOTES);
echo "$value becomes ".preg_replace($patterns,$replacements,$value)."\n";
}
echo "</pre>";
?>
输出看起来像
5' 6" becomes 5' 6"
5ft 6in becomes 5' 6"
5 feet 6 inches becomes 5' 6"
5.5' becomes 5' 6"
5'6" becomes 5' 6"
5 1/2' becomes 5' 6"
3 foot 5 inches becomes 3' 5"
2ft 8in becomes 2' 8"
3 1/4in becomes 3.25"
3 1/4ft becomes 3' 3"