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
Top answer
1 of 6
2

YOu could explode the string into an array :

$list = explode(',', $string);
var_dump($list);

Which will give you :

array
  0 => string '22' (length=2)
  1 => string '23' (length=2)
  2 => string '24' (length=2)
  3 => string '25' (length=2)

Then, do whatever you want on that array ; like remove the entry you don't want anymore :

foreach ($list as value) {
    if ($value == $usrID) {
        unset($list[$key]);
    }
}
var_dump($list);

Which gives you :

array
  0 => string '22' (length=2)
  2 => string '24' (length=2)
  3 => string '25' (length=2)

And, finally, put the pieces back together :

$new_string = implode(',', $list);
var_dump($new_string);

And you get what you wanted :

string '22,24,25' (length=8)

Maybe not as "simple" as a regex ; but the day you'll need to do more with your elements (or the day your elements are more complicated than just plain numbers), that'll still work :-)


EDIT : and if you want to remove "empty" values, like when there are two comma, you just have to modifiy the condition, a bit like this :

foreach ($list as value) {
    if ($value == $usrID || trim($value)==='') {
        unset($list[$key]);
    }
}

ie, exclude the $values that are empty. The "trim" is used so $string = "22,23, ,24,25"; can also be dealt with, btw.

2 of 6
2

Another issue is if you have a user 5 and try to remove them, you'd turn 15 into 1, 25 into 2, etc. So you'd have to check for a comma on both sides.

If you want to have a delimited string like that, I'd put a comma on both ends of both the search and the list, though it'd be inefficient if it gets very long.

An example would be:

$receivers = substr(str_replace(','.$usrID.',', ',', ','.$string.','),1,-1);
Top answer
1 of 4
7

I would prefer to use method 1 as its cleaner and more organised also Method 1 gives opportunity to use pairs from other source eg: bad words table in database. Method 2 would require another loop of sort..

<?php
$time_start = microtime(true);
for(i<=1000000;$i++){
    // Method 1
    $phrase  = "You should eat fruits, vegetables, and fiber every day.";
    $healthy = array("fruits", "vegetables", "fiber");
    $yummy   = array("pizza", "beer", "ice cream");
    $phrase = str_replace($healthy, $yummy, $phrase);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did Test 1 in ($time seconds)\n<br />";



$time_start = microtime(true);
for(i<=1000000;$i++){
    // Method2
    $phrase  = "You should eat fruits, vegetables, and fiber every day.";
    $phrase = str_replace("fruits", "pizza", $phrase);
    $phrase = str_replace("vegetables", "beer", $phrase);
    $phrase = str_replace("fiber", "ice cream", $phrase);

}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did Test 2 in ($time seconds)\n";
?>  

Did Test 1 in (3.6321988105774 seconds)

Did Test 2 in (2.8234610557556 seconds)


Edit: On further test string repeated to 50k, less iterations and advice from ajreal, the difference is so miniscule.

<?php
$phrase  = str_repeat("You should eat fruits, vegetables, and fiber every day.",50000);
$healthy = array("fruits", "vegetables", "fiber");
$yummy   = array("pizza", "beer", "ice cream");

$time_start = microtime(true);
for(i<=10;$i++){
    // Method 1
    $phrase = str_replace($healthy, $yummy, $phrase);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did Test 1 in ($time seconds)\n<br />";



$time_start = microtime(true);
for(i<=10;$i++){
    // Method2
    $phrase = str_replace("fruits", "pizza", $phrase);
    $phrase = str_replace("vegetables", "beer", $phrase);
    $phrase = str_replace("fiber", "ice cream", $phrase);

}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did Test 2 in ($time seconds)\n";
?>  

Did Test 1 in (1.1450328826904 seconds)

Did Test 2 in (1.3119208812714 seconds)

2 of 4
4

Even if old, this benchmark is incorrect.

Thanks to anonymous user:

"This test is wrong, because when test 3 starts $phrase is using the results of test 2, in which there is nothing to replace.

When i add $phrase = "You should eat fruits, vegetables, and fiber every day."; before test 3, the results are: Did Test 1 in (4.3436799049377 seconds) Did Test 2 in (5.7581660747528 seconds) Did Test 3 in (7.5069718360901 seconds)"

        <?php
        $time_start = microtime(true);

        $healthy = array("fruits", "vegetables", "fiber");
        $yummy   = array("pizza", "beer", "ice cream");

        for(i<=1000000;$i++){
            // Method 1
            $phrase  = "You should eat fruits, vegetables, and fiber every day.";
            $phrase = str_replace($healthy, $yummy, $phrase);
        }
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "Did Test 1 in ($time seconds)<br /><br />";



        $time_start = microtime(true);
        for(i<=1000000;$i++){
            // Method2
            $phrase  = "You should eat fruits, vegetables, and fiber every day.";
            $phrase = str_replace("fruits", "pizza", $phrase);
            $phrase = str_replace("vegetables", "beer", $phrase);
            $phrase = str_replace("fiber", "ice cream", $phrase);

        }
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "Did Test 2 in ($time seconds)<br /><br />";




        $time_start = microtime(true);
        for(i<=1000000;$i++){
                foreach ($healthy as v) {
                  if (strpos($phrase, $healthy[$k]) === FALSE)  
                  unset($healthy[yummy[$k]);
                }                                          
                if ($healthy) $new_str = str_replace($healthy, $yummy, $phrase);

        }
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "Did Test 3 in ($time seconds)<br /><br />";

        ?>  

Did Test 1 in (3.5785729885101 seconds)

Did Test 2 in (3.8501658439636 seconds)

Did Test 3 in (0.13844394683838 seconds)

Top answer
1 of 5
6

Do you want this for generating slug?

Then you can do something like this:

$slugified = preg_replace('/[^-a-z0-9]+/i', '-', strtolower(trim($url)));

It will strip leading and trailing whitespace first, convert the string to lowercase, then replace all non-word characters (not a-z, 0-9 or -) with a single -

A    Beautiful *# Day will become a-beautiful-day

Remove strtolower() if you don't mind uppercase letters in the slug.

2 of 5
5

To generate slugs from any string (including strings with crazy UTF-8 characters), I use the following (taken from the WordPress source code). I realise it requires a little more code than the other answers posted here, but this is by far the most robust and complete solution to generating slugs automatically.

First, we need a remove_accents() function to convert all accent characters to ASCII characters, e.g. turn á into a.

/**
 * Converts all accent characters to ASCII characters.
 *
 * If there are no accent characters, then the string given is just returned.
 *
 * @param string $string Text that might have accent characters
 * @return string Filtered string with replaced "nice" characters.
 */
function remove_accents($string) {
 if (!preg_match('/[\x80-\xff]/', $string))
  return $string;
 if (seems_utf8($string)) {
  $chars = array(
  // Decompositions for Latin-1 Supplement
  chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
  chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
  chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
  chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
  chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
  chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
  chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
  chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
  chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
  chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
  chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
  chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
  chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
  chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
  chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
  chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
  chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
  chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
  chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
  chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
  chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
  chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
  chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
  chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
  chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
  chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
  chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
  chr(195).chr(191) => 'y',
  // Decompositions for Latin Extended-A
  chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
  chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
  chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
  chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
  chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
  chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
  chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
  chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
  chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
  chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
  chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
  chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
  chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
  chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
  chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
  chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
  chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
  chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
  chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
  chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
  chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
  chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
  chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
  chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
  chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
  chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
  chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
  chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
  chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
  chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
  chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
  chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
  chr(197).
Top answer
1 of 2
7

A new line and an n are two completely different characters.

New line is almost always represented as \n in programming languages, however, it is actually just character code 10 (decimal). n, on the other hand, is character code 110 (decimal). Other than the escape sequence, there is no relation at all between them.

To replace \n and only \n use:

$str = str_replace("\n", " ", $string);

Edit 1: For an example, see the snippet posted by Conner in the comment.

Edit 2: As noted by JoeCortopassi, PHP will only parse \n inside of certain types of string literals. For example, '\n' will end up in a literal \n string. (Character \ followed by character n).

"\n" //new line (char code 10)
'\n' //literal \ followed by n (char code 92 followed by char code 110)
"\\n" //literal \ followed by n (same as '\n')
'\\n' //literal \ followed by n (same as '\n' :p)

Or:

"\n" === chr(10)
'\n' === "\\n" === '\\n' === chr(92) . chr(110)
2 of 2
3

Option #1

If you have a string that is being displayed like so:

see\nyou\nsoon\n

...then you will want to use this:

$str = str_replace('\n', ' ', $string);

...because PHP will is displaying the backslash character followed by the character for 'n'. You use single quotes, because php interprets that as a literal string with no escaped characters.

`

Option #2

If, on the other hand, you have a string that is being displayed like so:

see
you
soon

...then you will want to use this:

$str = str_replace("\n", '', $string);

...because php will look at this string as something that needs to be interpreted. This means that variables will be processed, and escaped characters like \n, \t and \\ will be interpreted into their literal character values.

From what you're talking about, it sounds like your strings are litered with what should be new line statements, but that were never interpreted, so option #1 is probably you're best bet

Find elsewhere