pneudralics Posted October 12, 2008 Share Posted October 12, 2008 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> Link to comment https://forums.phpfreaks.com/topic/128066-solved-why-is-preg_replace-taking-out-the-white-space-in-between-the-strings/ Share on other sites More sharing options...
DamienRoche Posted October 12, 2008 Share Posted October 12, 2008 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. Link to comment https://forums.phpfreaks.com/topic/128066-solved-why-is-preg_replace-taking-out-the-white-space-in-between-the-strings/#findComment-663198 Share on other sites More sharing options...
pneudralics Posted October 12, 2008 Author Share Posted October 12, 2008 Thanks you rock. It's been more than a year and just getting back to php. Link to comment https://forums.phpfreaks.com/topic/128066-solved-why-is-preg_replace-taking-out-the-white-space-in-between-the-strings/#findComment-663199 Share on other sites More sharing options...
DamienRoche Posted October 12, 2008 Share Posted October 12, 2008 You left php for a year? 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! Link to comment https://forums.phpfreaks.com/topic/128066-solved-why-is-preg_replace-taking-out-the-white-space-in-between-the-strings/#findComment-663202 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.