0

我想以我是一名学生的事实作为这个问题的序言,这是我的第一个 PHP 课程。所以,下面的问题可能有点新手......

好的,所以这个程序的重点是让我通过正则表达式过滤表单中的结果以及清理文本区域内容......

就目前而言,除了 strip_tags 位之外,一切正常。我将其设置为允许标签<b><p>,当我在文本区域中输入常规文本时,它会完美返回。如果我输入像<b>lucky</b>你这样的东西,返回的只是' b'。

我会发布我的代码。如果有人能帮我一把,我会很高兴的。在这一点上,我过度沮丧。我已经研究了我的导师提供的示例(我的几乎相同),并且我已经阅读了 PHP.net 手册,并且从我阅读的内容来看它应该可以工作......

工作代码位于http://www.lampbusters.com/~beckalyce/prog3b.php

<?php

if ( $_SERVER['REQUEST_METHOD'] == 'GET' )
{
    echo <<<STARTHTML
        <div class="content"><h1>Site Sign Up</h1>
        <h3>Enter Information</h3>
        <hr />
        <form method="post" action="$_SERVER[PHP_SELF]">
        <p>Full Name: <input type="text" name="fullName" size="30" /></p>
        <p>Password: <input type="password" name="password" size="30" maxlength="12" /></p>
        <p>Email: <input type="text" name="email" size="30"/></p>
        <p>Tell us about yourself:<br />
        <textarea name="aboutYou" rows="5" cols="40"></textarea><br />
        <input type="submit" name="submitted" value="submit" />&nbsp;<input type="reset" /></p>
        </form></div>
STARTHTML;

}
elseif ( $_SERVER['REQUEST_METHOD'] == 'POST')
{

    $errors = array();

    $dirtyName = $_POST['fullName'];
    $filterName = '/(\w+ ?){1,4}/';
    if (preg_match($filterName, $dirtyName, $matchName))
    {
        $cleanedName = ucwords(strtolower(trim(strip_tags(stripslashes($matchName[0])))));
    }
    else
    {
        $errors[] = "Enter a valid name. <br />";
    }

    $dirtyPass = $_POST['password'];
    $filterPass = '/[a-zA-Z0-91@#$%^&*]{8,12}/';
    if (preg_match($filterPass, $dirtyPass, $matchPass))
    {
        $cleanedPass = $matchPass[0];
    }
    else
    {
        $errors[] = "Enter a valid password. <br />";
    }

    $dirtyEmail = $_POST['email'];
    $filterEmail = '/^(?:\w+[.+-_]?){1,4}(?:\w+)@(?:\w+\.){1,3}\w{2,4}/';
    if (preg_match($filterEmail, $dirtyEmail, $matchEmail))
    {
        $cleanedEmail = $matchEmail[0];
    }
    else
    {
        $errors[] = "Enter a valid email address. <br />";
    }

    $dirtyText = $_POST['aboutYou'];
    $filterText = '/((\w+)[ ."\'?!,-]{0,3})+/';
    if (preg_match($filterText, $dirtyText, $matchText))
    {
        $validText = $matchText[0];
        $ignore = '<b><p>';
        $notags = strip_tags($validText,$ignore);
        $cleanedText = preg_replace('/fuck|shit|ass|bitch|android/i',"*****",$notags);
    }
    else
    {
        $errors[] = "Enter information about yourself. <br />";
    }

    if (count($errors) == 0)
    {
        echo <<<STARTHTML2
            <div class="content"><h1>Site Sign Up</h1>
            <h3>Verify your information</h3>
            <hr />
            Name: <span class="choices"> $cleanedName <br /></span>
            Password: <span class="choices">$cleanedPass <br /></span>
            Email: <span class="choices">$cleanedEmail <br /></span>
            About you: <span class="choices">$cleanedText <br /></span>
STARTHTML2;

    }
    else
    {
        echo "<div class=\"content\">Please correct the following errors:<br />\n";
        $errnum = 1;
        foreach ($errors as $inderr)
        {
            echo "$errnum. $inderr";
            $errnum++;
        }
    }
    echo '<br /><a href="prog3.php">Back to Form</a>';
    echo '</div>';
    echo '<p style="text-align: center">' . date('l, F d, Y') . '</p>';
}
?>
4

1 回答 1

0

看起来您的正则表达式不允许使用 < 和 > 字符,而且,如果它旨在匹配整个文本,它应该以 ^ 开头并以 $ 结尾,否则它只会匹配一小部分尽可能根据模式输入,这很可能在提供时简单地在 $match[0] 中返回 'b'<b>TextHere

于 2011-10-23T02:13:42.067 回答