replace all character in string php code example

Example 1: php replace return character

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

Example 2: Replace String in PHP

phpCopy<?php
$mystring = "This is my string.";
echo("This is the string before replacement: ");
echo($mystring);
echo("\n");
$mynewstring = str_replace(" my ", " ", $mystring);
echo("Now, this is the string after replacement: ");
echo($mynewstring);
?>

Example 3: php replace string

str_replace ($needle, $replacement, $haystack);

Example 4: Replace String in PHP

phpCopy<?php
$mystring = "This is my string.";
echo("This is the string before replacement: ");
echo($mystring);
echo("\n");
$mynewstring = str_replace(" my ", " ", $mystring, $count);
echo("Now, this is the string after replacement: ");
echo($mynewstring);
echo("\n");
echo("The number of replacements is: ");
echo($count);
?>

Tags:

Php Example