phpreplacenewline

str_replace() with multi-line string is failing to match a visibly identical substring


This is my string:

$a = "RSS
here: 
Your result: 3 points form 50 example.";

How to delete "RSS here:" from this string?

My try:

$result = str_replace("RSS
here: 
Your result: 3 points form 50 example.", " " ,$a);
echo $result;

Solution

  • Use \r\n for carriage return + line feed eg new line:

    $a = "RSS
    here: 
    Your result: 3 points form 50 example.";
    
    $result = str_replace("RSS\r\nhere:", "" ,$a);
    echo $result;
    

    Result:

    Your result: 3 points form 50 example.