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?