正则表达式踢出 PHP preg_match if 条件中的 HTML 文本区域输入

发布于 2024-12-29 12:04:20 字数 2394 浏览 3 评论 0原文

我不明白为什么我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卷耳 2025-01-05 12:04:20

使用 nl2br() 将新行替换为 < br /> 标签,而不是尝试使用 preg_replace() 重新发明轮子。

也就是说,您的 preg_replace() 调用对我有用:

$str = <<<STR
foo
bar
baz
STR;

var_dump($str);
$str = preg_replace("/[\r\n]+/", "<br />", $str);
var_dump($str);

输出:

string(11) "foo
bar
baz"
string(21) "foo<br />bar<br />baz"

Use nl2br() to replace new lines with <br /> tags, instead of trying to reinvent the wheel with preg_replace().

That said, your preg_replace() call works for me:

$str = <<<STR
foo
bar
baz
STR;

var_dump($str);
$str = preg_replace("/[\r\n]+/", "<br />", $str);
var_dump($str);

Output:

string(11) "foo
bar
baz"
string(21) "foo<br />bar<br />baz"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文