title: Regular Expressions kicking out HTML textarea input in PHP preg_match if condition
body: <p>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:</p>
  
  <p>Here is the relevant PHP:</p>
  
  <pre><code>/* 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 '&lt;p&gt;&lt;font color="red" size="+1"&gt;
        Please enter an ingredient description!&lt;/font&gt;&lt;/p&gt;';
  }
  </code></pre>
  
  <p>The custom function escape_data is as follows:</p>
  
  <pre><code>$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;
  } 
  </code></pre>
  
  <p>Here is the relevant HTML that calls for the textarea input:</p>
  
  <pre><code>&lt;form action="ingredient_form.php" method="post" enctype="multi-part/form-data"&gt;
  &lt;fieldset&gt;
  
  &lt;p&gt;&lt;b&gt;Ingredient Description:&lt;/b&gt;&lt;/p&gt; 
      &lt;p&gt;&lt;textarea cols="100" rows="20" name="ingredient_descrip"&gt;&lt;/textarea&gt;&lt;?php if (isset($_POST['ingredient_descrip'])) echo $_POST['ingredient_descrip']; ?&gt;&lt;/p&gt;
  
  
  &lt;/fieldset&gt;
  &lt;div align="center"&gt;&lt;input type="submit" name="submit" value="Submit Proposed Ingredient Profile" /&gt;&lt;/div&gt;
  &lt;input type="hidden" name="submitted" value="TRUE" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>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:</p>
  
  <pre><code>if ($_POST['ingredient_descrip']) 
  {
  $idscr = stripslashes(trim($_POST['ingredient_descrip']));
  echo "&lt;p&gt;" .$idscr. "&lt;/p&gt;";
  $idscr = escape_data($idscr);
  echo "&lt;p&gt;" .$idscr. "&lt;/p&gt;";
  $idscrp = preg_replace("/[\r\n]+/", "&lt;br /&gt;", $idscr);
  
  echo "&lt;p&gt;" .stripslashes($idscrp). "&lt;/p&gt;";
  }                
  </code></pre>
  
  <p>The preg_replace doesn't seem to be doing its job either.  I put this into the textarea for testing purposes:</p>
  
  <pre><code>This is a (test)
  Why isn't it working?
  but
  </code></pre>
  
  <p>My echo results come back as:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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?</p>