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 Overflow
🌐
PHP
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.
🌐
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 ...
🌐
ItSolutionstuff
itsolutionstuff.com › post › how-to-replace-n-with-br-in-phpexample.html
How to Replace \n with br in PHP? - ItSolutionstuff.com
May 14, 2024 - Then after when he wants to show ... so you have to convert "\n" to newline. so here we will use nl2br() function and str_replace() function to replace \n to br....
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-str_replace-function
PHP str_replace() Function - GeeksforGeeks
November 30, 2021 - This parameter specifies the string or array of strings which we want to search for $searchVal and replace with $replaceVal. $count: This parameter is optional and if passed, its value will be set to the total number of replacement operations ...
Find elsewhere
🌐
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 ...
🌐
GitHub
gist.github.com › VijayaSankarN › 0d180a09130424f3af97b17d276b72bd
String replace nth occurrence using PHP · GitHub
String replace nth occurrence using PHP · Raw · str_replace_n.php · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
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 ...
🌐
Udemy
blog.udemy.com › home › it & development › web development › how to use the php str_replace function to find and replace strings
How to Use the PHP STR_REPLACE Function - Udemy Blog
April 14, 2026 - The PHP `str_replace()` function searches a string or array and replaces every matching instance with a new value. This article covers its syntax, parameters, case sensitivity, empty-string replacements, and array usage.
🌐
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; ?>