PHP表单问题

时间:2010-09-13 23:27:06

标签: php

有没有办法可以检查用户是否使用PHP在表单中输入<p>标记?

7 个答案:

答案 0 :(得分:1)

如果已发布,您可以执行类似'&lt; p&gt;'的strstr或其中一个类似的函数,如果它存在则返回该位置,如果不存在则返回NULL。

<?php if ( strstr ( $body, '<p>' ) == NULL )
echo 'All Clear';
else die ( 'Contains <p>' );

答案 1 :(得分:1)

if(empty($_POST['foo'])) {
   print "Foo empty";
} else { 
   if(stristr($_POST['foo'], '<p>')) {
      print "Contains P tag";
   } else {
      print "No P tag";
   }
}

答案 2 :(得分:1)

如果您只是想剥离所有标记使用:

strip_tags ( string $str [, string $allowable_tags ] )

否则:

substr_replace ( $string , string $replacement , int $start [, int $length ] )

取决于为什么你知道什么

1 - PHP.net

答案 3 :(得分:0)

您可以使用javascript或jquery .onFocus事件。

答案 4 :(得分:0)

假设他们没有输入像<p class="stuff">这样的任何内容,您可以使用简单的strpos()来电:

$text = $_POST['name_of_field'];
if (strpos($text, '<p>') !== FALSE) {
    die("No <p> tags allowed");
}

如果他们输入属性,那么你很可能需要一个正则表达式,它有一系列问题:

$text = $_POST['name_of_field'];
if (preg_match('/<p.*?>/i', $text)) {
   die("No <p> tags allowed");
}

答案 5 :(得分:0)

这是什么意思?假设你有一个字符串变量中的表单内容,这样的东西应该工作:

<?php

ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL | E_STRICT);

$string1 = 'Hello <p> world';
$string2 = 'Hello world';

$foundIt1 = strripos($string1, '<p>');
$foundIt2 = strripos($string2, '<p>');

if (false === $foundIt1) {
    echo '1. didn\'t find it';
} else {
    echo "1. found it at offset $foundIt1";
}
echo "\n";

if (false === $foundIt2) {
    echo '2. didn\'t find it';
} else {
    echo "2. found it at offset $foundIt2";
}


?>

答案 6 :(得分:0)

如果您想要替换或删除它们:

$new_data = preg_replace("/<p>/", "whatever you want to replace it with here", $_POST['form_field_id_here']);

如果你只是想检查一下

strpos("<p>", $_POST['form_field_id_here']);

然后阅读此内容以确保您不会让您的网站向攻击者开放: What's the best method for sanitizing user input with PHP?

(编辑:我知道,我知道。没有HTML解析的正则表达式。恕我直言,如果你所做的只是检查

标签那么一点正则表达式比使用一个巨大的HTML解析器更好那就是说,如果你要检查许多标签和<p class="something">之类的东西,你应该看一下:http://docs.php.net/manual/en/domdocument.loadhtml.php