steve m Posted December 14, 2007 Share Posted December 14, 2007 Hello, I am having a problem trying to use the preg_replace() function. I have a form and I want to remove "new line(\n)", tabs(\t), carriage returns(\r) etc.. from the data being submitted, but it doesn't seem to work. <?php $str = preg_replace("/\n/", '' ", $_POST[email]); echo $str; ?> It doesn't seem to do anything. For example, if the email field is submitted with test@test.com\n and I print $str, it prints test@test.com\\n. Can someone please tell me what I am doing wrong? Quote Link to comment Share on other sites More sharing options...
VirusDoctor Posted December 14, 2007 Share Posted December 14, 2007 Hi, try this code: <?php $str = preg_replace("/\n/", '' ", $_POST['email']); echo $str; ?> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 14, 2007 Share Posted December 14, 2007 your code should be <?php $str = preg_replace("/[\t\n\r]/g", "", $_POST['email']); echo $str; ?> Quote Link to comment Share on other sites More sharing options...
steve m Posted December 14, 2007 Author Share Posted December 14, 2007 Hi rajivgonsalves, I tried that and it still doesn't remove the \n, \t, or \r. Am I not ecaping those characters right? Quote Link to comment Share on other sites More sharing options...
effigy Posted December 14, 2007 Share Posted December 14, 2007 If you run a proper test you can see that it works: <pre> <?php $data = "test@test.com\n"; echo '-', preg_replace("/\n/", '', $data), '-'; ?> </pre> It appears you might be suffering from magic quotes. Quote Link to comment Share on other sites More sharing options...
steve m Posted December 14, 2007 Author Share Posted December 14, 2007 Hi effigy, I think magic quotes are on. So do do I need to run the POST variable through stripslashes() first? Quote Link to comment Share on other sites More sharing options...
kjtocool Posted December 14, 2007 Share Posted December 14, 2007 That, or, turn off magic quotes. Quote Link to comment Share on other sites More sharing options...
steve m Posted December 14, 2007 Author Share Posted December 14, 2007 I tried the stripslashes() before the preg_replace() and it still won't remove the \n. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.