Jump to content

[SOLVED] Why is preg_replace taking out the white space in between the strings??


Recommended Posts

I have a small form to update a question. I'm trying to only allow alphanumeric characters except for the question mark. Everytime I try updating my database through the form it takes all the space away..? What is causing that?

 

If I enter in...What cars do I like?

 

It'll save and output... WhatcarsdoIlike?

 

<?php
//Update question and answer
if (isset ($_POST['submit'])) {
$pquestion = ($_POST['question']);
$pquestion = preg_replace('/[^a-zA-Z0-9?]/', '', $pquestion);
$panswer = ($_POST['answer']);
$panswer = preg_replace('/[^a-zA-Z0-9]/', '', $panswer);

if ( (!empty ($pquestion)) && (!empty ($panswer)) ) {
//Update...
$updateqa = "UPDATE question SET question=\"$pquestion\", answer=\"$panswer\"";
$updateqaresult = mysql_query ($updateqa);
echo '<font color="red">Question and Answer has been successfully updated.</font><br /><br />';	
}
else {
echo '<font color="red">Question or Answer is empty.</font><br /><br />';	
}
}
?>

    <form action="question.php" method="post">
    Question <input type="text" name="question" size="50" maxlength="50" />
    <br />
    <br />
    Answer <input type="text" name="answer" size="20" maxlength="20" />
    <br />
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form> 

It's because this says everything BUT:

 

preg_replace('/[^a-zA-Z0-9]/', '', $panswer)

 

You need to put the whitespace char type in there: \s

 

So I think it should be:

 

preg_replace('/[^a-zA-Z0-9\s]/', '', $panswer)

 

Change your other preg_replace if need be as well. Hope that helps.

 

 

You left php for a year?  :o This conversation is over.

 

Seriously, I hope you soon get to grips with it again. I've only been doing it for about 2-3 months but this forum has been an absolute godsend!

 

Good luck for the future!

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.