Jump to content

Dealing with textarea fields with spaces


eMonk

Recommended Posts

How can I check if the text entered in a textarea field equals the text I have stored in a mysql table?

 

$submitted_description = trim(mysqli_real_escape_string($db,$_POST['description']));

$query = "SELECT description FROM pets";
$result = $db->query($query);
$row = $result->fetch_assoc();
$sql_description = $row['description'];

 

Here's what they echo:

 

submitted_description:

Testing...\r\n\r\n1\r\n\r\n2\r\n\r\n3

 

sql_description:

Testing... 1 2 3

 

I tried adding the following to get rid of \r\n from $submitted_description but they both still don't equal but echo the same text.

 

$submitted_description = str_replace(array("\r","\n",'\r','\n'),' ', $submitted_description);

 

They following code only matches when no spaces are entered in the textarea field.

 

if($submitted_description == $sql_description)
{
     echo "Match!";
}
else
{
     echo "Don't Match!";
}

 

How can this be done?

 

I also tried adding nl2br() to both variables but that didn't work either.

Link to comment
Share on other sites

Well, I'm making some assumptions because, without actually having the data in front of me, there may be some aspects that aren't apparent from your description.

 

You state the values are as follows

submitted_description:

Testing...\r\n\r\n1\r\n\r\n2\r\n\r\n3

 

sql_description:

Testing... 1 2 3

 

So, the value in the database is "Testing..." [space] "1" [space] "2" [space] "3"

 

And, it appears the user is entering "Testing..." [linebreak][linebreak] "1" [linebreak][linebreak] "2" [linebreak][linebreak] "3"

 

But, what is not clear is what ELSE would you allow the user to enter and still want to consider a match? Can the user put in tabs between words? How many linebreaks between words should you allow? What if the user enters multiple spaces, should they be collapsed to one space, etc. etc. . . . Plus, I don't know how all the other values in your database may be formatted such that different modifications may be necessary.

 

Without knowing all those answers here is my guess at a solution. Replace all repetitions of line breaks (\n or \r) and spaces with a single space. So, if you have "1" [space][linebreak][linebreak][space] "2" make the result "1" [space] "2".

 

$user_description = "Testing...\r\n\r\n1\r\n\r\n2\r\n\r\n3";
$sql_description  = "Testing... 1 2 3";

//Parse input
$user_description = preg_replace("#([\r|\n| ]+)#", ' ', $user_description);

if($user_description == $sql_description){
    echo 'Match'; // <== This will be the output for the above input values
} else {
    echo "Don't match";
}

Link to comment
Share on other sites

That didn't work either. The only one that removed the \r\n tags was:

 

$user_description = str_replace(array("\r","\n",'\r','\n'),' ', $user_description);

 

Even with the removed \r\n tags it still doesn't match. I also tried the following:

 

$user_description = str_replace("\r\n",'', $user_description);
$user_description = preg_replace("/[rn]+[st]*[rn]+/","n",$user_description);
$user_description = preg_replace("!\r|\n!m",'',$user_description);
$user_description = preg_replace('/\r\n|\r|\n/m','',$user_description);
$user_description = trim_chars('$user_description', '\r\n');
$user_description = trim($user_description, '\r\n');
$user_description = str_replace('\r\n', "<br/>", $user_description);
$user_description = preg_replace("/\r\n|\n|\r|rn/", "<br />", $user_description);

Link to comment
Share on other sites

The code I provided worked based upon the EXACT data you supplied above. That is why I prefaced my response as I did. I suspect there is something else going on that you are not aware of and so the information you are providing is not accurate.

 

Additionally the line that you say works might be a clue

$user_description = str_replace(array("\r","\n",'\r','\n'),' ', $user_description);

The escape characters in double quotes match the special line break characters which could be sent from a textarea input. But, the ones in the single-quotes are the literal text '\n' and '\r'. If your submitted descriptions have the literal characters '\r' and '\n' then something is wrong with your input. You shouldn't "see" those actual characters (unless you are actually entering them). So, you need to fix what is causing those to show up.

 

What is that last block of code supposed to be? If that is your process of sanitizing the data you are definitely making it more complicated than it should be. But, I think your problem is this line

$user_description = trim_chars('$user_description', '\r\n');

If that line is supposed to be replacing something with a line break it is actually replacing it with the literal characters '\r\n'.

Link to comment
Share on other sites

This is creating the \r\n tags...

 

<textarea name=\"description\" cols=\"70\" rows=\"15\">$description</textarea>

 

The echoed results include the tags automatically.

 

I can get the variables to match but not if it includes line breaks for some reason.

Link to comment
Share on other sites

You didn't answer my questions. An input field does not create '\r\n' tags. Again, are you saying the input contains actual line breaks are are you actually seeing the characters '\r\n'? And, what was that last block of code you posted? Was that a list of attempts to manipulate the input or is that code you have in place before you try and do the matching?

 

You do realize that '\r\n' (in single quotes) is entirely different than "\r\n" (in double quotes), right? When you put those into single quotes it means the literal characters "\" "r" "\" "n". However, when you put those in double quotes it represents line break characters.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.