防止双引号被剥夺

时间:2013-03-28 05:31:42

标签: php

以脚和英寸作为字符串显示人物身高的最佳方法是什么,还可以防止sql注入以及确保正确的输入格式?理想情况下,我想以5'11“为例进行显示。

$height = $_POST['height'];
$height = stripslashes($height);

这个问题是,虽然在MySQL中它存储为5'11“,当它在页面上输出时,它显示为5'11而不是最后的双引号。

有更好的方法吗?我也在考虑将高度分成两个单独的文本字段输入,一个用于英尺,一个用于英寸..然后将两个组合显示为一个。

连连呢?

2 个答案:

答案 0 :(得分:1)

要显示您需要转义它们的引号:

echo "5\' 11\"";

将输出:

5' 11"

在插入数据库之前,您可以使用addslashes来转义所有字符(需要转义)。然后,为了增加安全性,您应该查看prepared statements

答案 1 :(得分:0)

您可以通过一点创造力过滤内容,使其保持一致。在这个例子中,我用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>";
?>

输出看起来像

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"
相关问题