dongtuo4132 2012-01-27 16:01
浏览 52

如果条件,正则表达式在PHP preg_match中踢出HTML textarea输入

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("/[
]+/", "<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) 
Why isn\'t it working? 
but

This is a (test) rnWhy isn't it working? rnbut

The preg_replace should be substituting the with a break and then the stripslashes should get rid of the remainin escape characters. What am I missing?

  • 写回答

1条回答 默认 最新

  • donglou8371 2012-01-27 16:26
    关注

    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("/[
    ]+/", "<br />", $str);
    var_dump($str);
    

    Output:

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

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用