Move string and replace a specific PHP character

6

I wanted to replace, in a string, all occurrences of the & character with a e (or remove it). The character may be in the middle of a word or appear multiple times in the same string.

Example:

  

Original value: John went to the Hotel & Spa to spend a holiday.

     

After replacing: John went to the Hotel and Spa to spend a vacation.

    
asked by anonymous 26.05.2014 / 10:09

1 answer

15

You can use str_replace () , the parameters of this function are:

  

str_replace (search, substitute, source, $ occurrences)

     

search - text to search for substitute - text that should replace what is found in search
< strong> source - the string where the content to be replaced is $ occurrences (optional) - this variable will receive the value of how many times replace was executed and the text found / replaced.

An example would be:

$string = "O João foi ao Hotel & Spa passar férias";
$stringCorrigida = str_replace('&', 'e', $string);
echo $stringCorrigida; // resultado: O João foi ao Hotel e Spa passar férias
    
26.05.2014 / 10:46