dongqindan4406 2011-10-23 01: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-23 02: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

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

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛