Jump to content

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


pneudralics

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.