Replace spaces with characters

2

For the moment I have been able to deal with this:

 $link = "";
 $longitud = strlen($pais);

 for($i = 0; $i<$longitud;$i++){
     if($pais[$i] == ""){
         $link .= '+';
     }else{
         $link .= $pais[$i];
     }
 }

The result would be, for example, if $pais = "Estados Unidos de America" variable $link = "Estados+Unidos+de+America"

My doubt is: there is some function to which you can indicate a delimiter and a character or string of characters to substitute this delimiter type explode() but indicating a substitution character

    
asked by gmarsi 13.06.2017 в 11:52
source

1 answer

4

If you want to generate a URL you can use urlencode .

If you want to do something like what Wordpress does to have a slug you can use something like this:

$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);

In that function you can select what would be the "separator". Actually the function preg_replace gives you a lot of game

    
answered by 13.06.2017 / 12:07
source