php replace spaces code example

Example 1: php replace spaces with dash

str_replace(' ', '-', $string);

Example 2: javascript replace line breaks with br

function nl2br(str){
 return str.replace(/(?:\r\n|\r|\n)/g, '<br>');
}
var stringWithNewLines= `Well this is a
a very broken
sentance`;
var stringWithBrs=nl2br(stringWithNewLines);
// stringWithBrs= "Well this is a<br>a very <br>broken<br>sentance"

Example 3: php replace space with 20

//Replace space with %20 for url to understand
$new = str_replace(' ', '%20', $your_string);

//Alternate for URL use, use this
urlencode ( string $str )

Example 4: remove empty spaces php

$string = str_replace(' ', '', $string);

Example 5: php string replace space

$journalName = preg_replace('/\s+/', '_', $journalName);