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 [email protected]\n and I print $str, it prints [email protected]\\n. Can someone please tell me what I am doing wrong? Link to comment https://forums.phpfreaks.com/topic/81628-solved-problem-with-using-preg_replace/ 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; ?> Link to comment https://forums.phpfreaks.com/topic/81628-solved-problem-with-using-preg_replace/#findComment-414663 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; ?> Link to comment https://forums.phpfreaks.com/topic/81628-solved-problem-with-using-preg_replace/#findComment-414664 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? Link to comment https://forums.phpfreaks.com/topic/81628-solved-problem-with-using-preg_replace/#findComment-414848 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 = "[email protected]\n"; echo '-', preg_replace("/\n/", '', $data), '-'; ?> </pre> It appears you might be suffering from magic quotes. Link to comment https://forums.phpfreaks.com/topic/81628-solved-problem-with-using-preg_replace/#findComment-414885 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? Link to comment https://forums.phpfreaks.com/topic/81628-solved-problem-with-using-preg_replace/#findComment-414904 Share on other sites More sharing options...
kjtocool Posted December 14, 2007 Share Posted December 14, 2007 That, or, turn off magic quotes. Link to comment https://forums.phpfreaks.com/topic/81628-solved-problem-with-using-preg_replace/#findComment-414905 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. Link to comment https://forums.phpfreaks.com/topic/81628-solved-problem-with-using-preg_replace/#findComment-414929 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.