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 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C