November 7, 200817 yr Hi there, I'm trying to remove the some expressions from a string using regex. It's working fine except for anything like \n, \r etc. This is the code I'm using: <?php echo preg_replace("/[\n|\r|\t]/","",$email); ?> I've tried it with a million different kinds of syntax and also str_replace and it don't work, but every site I've looked at says it should. I'm very stuck Many thanks if anyone can help! Kirsty
November 7, 200817 yr hate to tell you this but that works... so check what the input your giving it actually contains echo preg_replace("/[\n|\r|\t]/","","my string\nhas new lines \n\r");
November 7, 200817 yr Author hate to tell you this but that works... so check what the input your giving it actually contains echo preg_replace("/[\n|\r|\t]/","","my string\nhas new lines \n\r"); Aye it works if you type that string into the php and run it, but if I paste the same string into an input in a form and hit submit it leaves the newline intact but puts this in front: \ How can that be?? Is there something wrong with the rest of my code? There's not much of it: <?php if (isset($_REQUEST['submit'])) {$email = ($_REQUEST['email']); echo preg_replace("/[\n|\r|\t]/","","$email");} ?> Any ideas?
November 7, 200817 yr the server escaped the slashes, try this <?php if (isset($_REQUEST['submit'])) {$email = ($_REQUEST['email']); echo preg_replace("/[\\n|\\r|\\t]/","","$email");} ?>
November 7, 200817 yr Author the server escaped the slashes, try this <?php if (isset($_REQUEST['submit'])) {$email = ($_REQUEST['email']); echo preg_replace("/[\\n|\\r|\\t]/","","$email");} ?> No joy I'm afraid - it's still coming back exactly the same. Any more ideas would be most welcome though!
November 9, 200817 yr ok... in a input field if you type \n it wont show as a newline because its a \ & n... see each character has a Hexadecimal code a newline is xA0, the \ is x5C and n is x6E... so your regex is looking for xA0 (newline) where there is no newline.... on the other hand if you change the email input tobe a textarea each time you hit enter it inputs a newline (which both your regex and mine works on)... // below removes everything other than letters and numbers preg_replace('/[^\x20-\x7E\xA0-\xFF]/','',$email); its not working because its not getting the correct testing data also, you have magic_gpc (is that the right name?) turned on which escapes all the input from forms this is adding the \ .... you can use this to get rid or add slashes stripslashes($string); addslashes($string); hope that helps
Create an account or sign in to comment