dongqindan4406 2011-10-22 17:57
浏览 26
已采纳

使用strip_tags函数

I want to preface this question with the fact that I am a student and this is my first PHP class. So, the following question might be a bit novice...

Okay so the point of this program was for me to filter results from a form through regular expressions along with clean up the text area content...

Well as of right now, all works fine except for the strip_tags bit. I have it set to allow the tags <b> and <p>, and when I enter regular text into the text area, it returns perfectly. If I enter something such as <b>lucky</b> you, all that is returned is 'b'.

I'll post my code. If anyone can give me a hand, I'd love it. At this point I'm overly frustrated. I've studied the examples my instructor supplied (mine is almost identical) and I've looked throught the PHP.net manual and from what I read it should work...

The working code is at 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 />
";
        $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>';
}
?>

展开全部

  • 写回答

1条回答 默认 最新

  • douejuan9162 2011-10-22 18:13
    关注

    It doesn't look like your regular expression allows for the < and > characters, also, if it was meant to match the entire text, it should start with ^ and end with $, otherwise it will just match on a small section of the input as best it can according to the pattern which is likely what happened to simply return 'b' in $match[0] when supplying <b>TextHere

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?