简体   繁体   中英

How to Trim special Character \r in php

Can anybody tell me here how to trim the special character \\r\\n or \\n in php? I know there is already trim function in php but that is not working.

While copying the email address from some other place and copying it into the excel field and during uploading i am getting the invalid email address error. But the email id is correct just due to the special character \\r the error is showing. Any help will be appreciated.

Thanks in advance

Why don't you make use of a simple str_replace() if trim() really doesn't work (It works actually) ?

<?php
$email = 'john@gmail.com\r';
echo str_replace('\r','',$email);

use

$email = str_replace('\r', '', $email);

btw "\\r" is called carriage return char (you know old typewriters?)

From PHP docs for trim() :

This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters:

" " (ASCII 32 (0x20)), an ordinary space.
"\\t" (ASCII 9 (0x09)), a tab.
"\\n" (ASCII 10 (0x0A)), a new line (line feed).
"\\r" (ASCII 13 (0x0D)), a carriage return.
"\\0" (ASCII 0 (0x00)), the NUL-byte.
"\\x0B" (ASCII 11 (0x0B)), a vertical tab.

See example here: http://codepad.org/O9xa42cw

$string = "\nemail@email.com\n\r"; 
$string = str_replace(array("\n", "\r"), '', $string);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM