Replace multiple strings in PHP

0

I want to replace the result of several variables by PHP, each variable has a different value, I would like it if one value gives me a result that is changed by another.

$descarga1 > Esto me devuelve el valor > Mega.co.nz ó Dropbox.com
$descarga2 > Esto me devuelve el valor > Mediafire.com ó libros.relaxmind.info
$descarga3 > Esto me devuelve el valor > Googledrive.com o Box.net

To replace, I'm trying to do it this way:

function str_replace($descarga1,$descarga2,$descarga3){

str_replace(
    array("Mega.co.nz","Obtén el libro desde Mega.nz"),
    array("Dropbox.com", "Obtén el libro desde Dropbox.com"),
    array("Mediafire.com","Obtén el libro desde Mediafire.com"),
    array("libros.relaxmind.info", "Obtén el libro desde nuestra web"),
    array("Googledrive.com","Obtén el libro desde Google Drive"),
    array("Box.net", "Obtén el libro desde Box.net"),
);
return $

So far I have stuck, I hope you have given me to understand in the correct way, In summary I would like that if $descarga1, $descarga2 o $descarga3 host a result, this is changed by Get the book desde nombre del servidor .

I hope you can help me, Thanks!.

    
asked by Joseph Gonzaga 22.06.2017 в 03:29
source

1 answer

3

I will propose a slightly more "clean" form, maybe your function should receive the list and the element to be modified. taking into account the idea that if you want to change 10 values, will you send 10 parameters? , the function would have the following form, where we will use the function strtr that accepts as a parameter a array of type clave => valor .

function replaceAll($value,$list){
  return strtr($value, $list);
}

To use this function, you must create the list of options_completely adaptable to the options you may have_ and pass the word to replace.

$list = array("Mega.co.nz"=>"Obtén el libro desde Mega.nz",
           "Dropbox.com"=> "Obtén el libro desde Dropbox.com",
           "Mediafire.com"=>"Obtén el libro desde Mediafire.com",
           "libros.relaxmind.info"=> "Obtén el libro desde nuestra web",
           "Googledrive.com"=>"Obtén el libro desde Google Drive",
           "Box.net" => "Obtén el libro desde Box.net");

$descarga1 = "Box.net";
echo replaceAll($descarga1,$list);
$descarga2 = "libros.relaxmind.info";
echo replaceAll($descarga2,$list);
    
answered by 22.06.2017 / 04:45
source