You need to place the \n in double quotes. Inside single quotes it is treated as 2 characters '\' followed by 'n'
Try below code:
$s = "\n test@gmail.com \n";
$s = str_replace("\n", '', $s);
echo $s;
Answer from Manthan Dave on Stack OverflowPHP
php.net › manual › en › function.str-replace.php
PHP: str_replace - Manual
If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values.
Top answer 1 of 2
34
You need to place the \n in double quotes. Inside single quotes it is treated as 2 characters '\' followed by 'n'
Try below code:
$s = "\n test@gmail.com \n";
$s = str_replace("\n", '', $s);
echo $s;
2 of 2
5
You have to use double quotes. \n is not interpreted as newline with single quotes.
PHP str_replace - Basic PHP Tutorial on str_replace Method - YouTube
04:49
PHP String Replacement: str_replace, str_ireplace & substr_replace ...
02:55
Entendendo a função str_replace no PHP - YouTube
04:44
str_replace() function | Replace words in a string using PHP ? ...
06:45
str_replace() - multi parameter functions in php - YouTube
07:13
PHP beginners tutorial 25 - string functions, substr replace and ...
W3Schools
w3schools.com › php › func_string_str_replace.asp
PHP str_replace() Function
If find is an array and replace is a string, the replace string will be used for every find value · Note: This function is case-sensitive. Use the str_ireplace() function to perform a case-insensitive search. Note: This function is binary-safe. ... <?php $arr = array("blue","red","green","yellow"); print_r(str_replace("red","pink",$arr,$i)); echo "Replacements: $i"; ?> Try it Yourself »
W3Schools
www-db.deis.unibo.it › courses › TW › DOCS › w3schools › php › func_string_str_replace.asp.html
PHP str_replace() Function
PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP SimpleXML PHP String PHP XML PHP Zip PHP Timezones ... The str_replace() function replaces some characters with some other characters in a string.
PHP Tutorial
phptutorial.net › home › php tutorial › php str_replace
PHP str_replace - PHP Tutorial
April 7, 2025 - As shown in the output, the str_replace() doesn’t change the input string but returns a new string with the substring 'Hello' replaced by the string 'Hi'; The following example uses the str_replace() function to replace the substring 'bye' with the string 'hey' in the string 'bye bye bye': <?php $str = 'bye bye bye'; $new_str = str_replace('bye', 'hey', $str); echo $new_str; // hey hey heyCopy
ReqBin
reqbin.com › code › php › zcmyga8t › php-string-replace-example
How do I replace a string in PHP?
January 14, 2023 - To replace a string with a substring in PHP, use the str_replace($search, $replace, $subject, $count) function. The str_replace() function takes the string to search for as the first argument, the string to replace the old value with as the ...
Programmer Notes
prognotes.net › home › php programming › php str_replace tutorial: replace strings the easy way
PHP str_replace Tutorial: Replace Strings the Easy Way
December 26, 2025 - PHP str_replace is a built-in function that replaces all occurrences of a search string with a replacement in the given subject (string or array). It’s designed for straightforward text substitutions and is an essential part of any PHP ...
OnlinePHP
onlinephp.io › str-replace › manual
str_replace - OnlinePHP.io Example
PHP 4, PHP 5, PHP 7, PHP 8 · str_replace - Replace all occurrences of the search string with the replacement string · Str Replace Online Tool · Manual · Code Examples · str_replace( array|string$search, array|string$replace, string|arr...
Arievisser
arievisser.com › blog › replace-multiple-items-from-string-text-in-php-with-array
Replace Multiple Items From String Text in PHP With Array | Arie Visser
When you give an array for $search and $replace, each nth element from the $search array that is found in the $subject string, will be replaced by the nth item from the $replace array. You can combine this with the array_keys function to replace multiple items from a string with an associative ...
BCCNsoft
doc.bccnsoft.com › docs › php-docs-7-en › function.str-replace.html
Replace all occurrences of the search string with the replacement string
(PHP 4, PHP 5, PHP 7) str_replace — Replace all occurrences of the search string with the replacement string · mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) This function returns a string or an array with all occurrences of search in subject replaced ...
Codecademy
codecademy.com › docs › php › string functions › str_replace()
PHP | String Functions | str_replace() | Codecademy
July 28, 2023 - The str_replace() function returns a string with occurrences of a specified substring replaced by another string.
Reintech
reintech.ai › blog › comprehensive-guide-php-str-replace-function
A Comprehensive Guide to PHP's `str_replace()` Function
April 14, 2023 - Choose str_replace() for literal string matching—it executes faster than regex alternatives and avoids the complexity of pattern syntax. For pattern-based replacements, complex matching rules, or case-insensitive operations, consider these alternatives: // Case-insensitive replacement $text = "PHP is great. php is powerful. Php is versatile."; $normalized ...
GeeksforGeeks
geeksforgeeks.org › php › remove-new-lines-from-string-in-php
Remove new lines from string in PHP - GeeksforGeeks
July 17, 2024 - <?php $text = "Hello \nwelcome to \ngeeksforgeeks"; echo $text; echo "\n"; $text = str_replace("\n", "", $text); echo $text; ?>