Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Removing newline characters

Featured Replies

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

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");

  • 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?

the server escaped the slashes, try this

<?php

if (isset($_REQUEST['submit']))
{$email = ($_REQUEST['email']);
echo preg_replace("/[\\n|\\r|\\t]/","","$email");}

?>

  • 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!

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.