正则表达式踢出 PHP preg_match if 条件中的 HTML 文本区域输入
我不明白为什么我的 PHP 代码 preg_match 在有括号或返回时不接受来自 HTML 文本区域输入的输入:
这是相关的 PHP:
/* Check for Ingredient Description */
if (preg_match ('%^\s*[A-Za-z0-9\,\.\' \-\"\(\)]{30,2048}$%', stripslashes(trim($_POST['ingredient_descrip']))))
{
$idscr = escape_data($_POST['ingredient_descrip']);
}
else
{
$idscr = FALSE;
echo '<p><font color="red" size="+1">
Please enter an ingredient description!</font></p>';
}
自定义函数 escape_data 如下:
$dbc = mysql_connect (DBHOST, DBUSER, DBPW)
function escape_data ($data)
{
if (function_exists('mysql_real_escape_string'))
{
global $dbc;
// Need the connection.
$data = mysql_real_escape_string (trim($data), $dbc);
$data = strip_tags($data);
}
else
{
$data = mysql_escape_string (trim($data));
$data = strip_tags($data);
}
// Return the escaped value.
return $data;
}
这是相关的 HTML调用文本区域输入:
<form action="ingredient_form.php" method="post" enctype="multi-part/form-data">
<fieldset>
<p><b>Ingredient Description:</b></p>
<p><textarea cols="100" rows="20" name="ingredient_descrip"></textarea><?php if (isset($_POST['ingredient_descrip'])) echo $_POST['ingredient_descrip']; ?></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit Proposed Ingredient Profile" /></div>
<input type="hidden" name="submitted" value="TRUE" />
我尝试删除 preg_match 并仅接受 textarea 输入,然后使用 preg_replace 函数重新获得格式并使用以下代码进行测试:
if ($_POST['ingredient_descrip'])
{
$idscr = stripslashes(trim($_POST['ingredient_descrip']));
echo "<p>" .$idscr. "</p>";
$idscr = escape_data($idscr);
echo "<p>" .$idscr. "</p>";
$idscrp = preg_replace("/[\r\n]+/", "<br />", $idscr);
echo "<p>" .stripslashes($idscrp). "</p>";
}
preg_replace 似乎也没有完成其工作。我将其放入文本区域以进行测试:
This is a (test)
Why isn't it working?
but
我的回显结果返回为:
This is a (test) Why isn't it working? but
This is a (test) \r\nWhy isn\'t it working? \r\nbut
This is a (test) rnWhy isn't it working? rnbut
preg_replace 应该用中断替换 \r\n ,然后 stripslashes 应该删除剩余的转义字符。我缺少什么?
I am at a loss on why my PHP code, the preg_match, is not accepting input from an HTML textarea input when it has parentheses or returns:
Here is the relevant PHP:
/* Check for Ingredient Description */
if (preg_match ('%^\s*[A-Za-z0-9\,\.\' \-\"\(\)]{30,2048}$%', stripslashes(trim($_POST['ingredient_descrip']))))
{
$idscr = escape_data($_POST['ingredient_descrip']);
}
else
{
$idscr = FALSE;
echo '<p><font color="red" size="+1">
Please enter an ingredient description!</font></p>';
}
The custom function escape_data is as follows:
$dbc = mysql_connect (DBHOST, DBUSER, DBPW)
function escape_data ($data)
{
if (function_exists('mysql_real_escape_string'))
{
global $dbc;
// Need the connection.
$data = mysql_real_escape_string (trim($data), $dbc);
$data = strip_tags($data);
}
else
{
$data = mysql_escape_string (trim($data));
$data = strip_tags($data);
}
// Return the escaped value.
return $data;
}
Here is the relevant HTML that calls for the textarea input:
<form action="ingredient_form.php" method="post" enctype="multi-part/form-data">
<fieldset>
<p><b>Ingredient Description:</b></p>
<p><textarea cols="100" rows="20" name="ingredient_descrip"></textarea><?php if (isset($_POST['ingredient_descrip'])) echo $_POST['ingredient_descrip']; ?></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit Proposed Ingredient Profile" /></div>
<input type="hidden" name="submitted" value="TRUE" />
I have tried removing the preg_match and just accepting the textarea input, then using preg_replace function to regain formatting and test with the following code:
if ($_POST['ingredient_descrip'])
{
$idscr = stripslashes(trim($_POST['ingredient_descrip']));
echo "<p>" .$idscr. "</p>";
$idscr = escape_data($idscr);
echo "<p>" .$idscr. "</p>";
$idscrp = preg_replace("/[\r\n]+/", "<br />", $idscr);
echo "<p>" .stripslashes($idscrp). "</p>";
}
The preg_replace doesn't seem to be doing its job either. I put this into the textarea for testing purposes:
This is a (test)
Why isn't it working?
but
My echo results come back as:
This is a (test) Why isn't it working? but
This is a (test) \r\nWhy isn\'t it working? \r\nbut
This is a (test) rnWhy isn't it working? rnbut
The preg_replace should be substituting the \r\n with a break and then the stripslashes should get rid of the remainin escape characters. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 nl2br() 将新行替换为
< br />
标签,而不是尝试使用preg_replace()
重新发明轮子。也就是说,您的
preg_replace()
调用对我有用:输出:
Use nl2br() to replace new lines with
<br />
tags, instead of trying to reinvent the wheel withpreg_replace()
.That said, your
preg_replace()
call works for me:Output: