Php 防止删除双引号

Php 防止删除双引号,php,Php,用英尺和英寸作为字符串显示人的身高、防止sql注入以及确保输入格式正确的最佳方法是什么?理想情况下,我想以5'11英寸为例显示它 $height = $_POST['height']; $height = stripslashes($height); 问题是,尽管在MySQL中它存储为5'11“,但当它在页面上输出时,它显示为5'11,末尾没有双引号 有更好的方法吗?我还考虑尝试将高度分为两个单独的文本字段输入,一个用于英尺,一个用于英寸。。然后将两者合并显示为一个 建议 要显示转义引号所需的

用英尺和英寸作为字符串显示人的身高、防止sql注入以及确保输入格式正确的最佳方法是什么?理想情况下,我想以5'11英寸为例显示它

$height = $_POST['height'];
$height = stripslashes($height);
问题是,尽管在MySQL中它存储为5'11“,但当它在页面上输出时,它显示为5'11,末尾没有双引号

有更好的方法吗?我还考虑尝试将高度分为两个单独的文本字段输入,一个用于英尺,一个用于英寸。。然后将两者合并显示为一个


建议

要显示转义引号所需的引号:

echo "5\' 11\"";
将输出:

5' 11"

在插入数据库之前,可以使用转义所有字符(需要转义的字符)。然后,为了增加安全性,您应该进行研究。

您可以通过一点创造性过滤内容,使其一致。在本例中,我使用htmlentities转换所有内容,但不必以这种方式将它们存储在数据库中。您需要确保在db注入之前为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
    '!&#34;!',
    '!&ldquo;!',
    '!&rdquo;!',
    '!&#8220;!',
    '!&#8221;!',
    '!&Prime;!',
    '!&#8243;!',
    '!in(ch(es)?|\.)?!',

    //Single Quotes
    '!&acute;!',
    '!&lsquo;!',
    '!&#[0]?39;!',
    '!&rsquo;!',
    '!&#8216;!',
    '!&#8217;!',
    '!&#8242;!',
    '!&prime;!',
    '!f(oo|ee)?t\.?!',

    //Conversions
    '!( 1/2|\.5)&apos;!',
    '!( 1/4|\.25)&apos;!',
    '!( 1/3|\.3(3(3)?)?)&apos;!',
    '!( 3/4|\.75)&apos;!',

    //cleanup
    '! (&)!',
    '!;([0-9])!',

    //fraction to decimal inch conversions
    '! 1/2!','! 1/4!','! 1/3!','! 3/4!',

    );

    $replacements = array(
    '&quot;','&quot;','&quot;','&quot;','&quot;','&quot;','&quot;','&quot;',
    '&apos;','&apos;','&apos;','&apos;','&apos;','&apos;','&apos;','&apos;','&apos;',
    '&apos; 6&quot;','&apos; 3&quot;','&apos; 4&quot;','&apos; 9&quot;',"$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>";
?>

显示在页面上显示它的代码。它可能需要使用
htmlentities()
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"