Function in PHP to replace words

0

I would like to see if you can help me with the following, I would like to know if it is possible to do a PHP function to replace some words. I am using str_replace as follows:

$mensaje = "Hola!";
$mensaje = str_replace($mensaje, "Hola!", "Buen dia, Vuelve pronto");

With this I get this:

  

Good morning, come back soon

I would like to know how I can do to replace several $mensaje with different result. Here something of what I'm looking for.

$mensaje1 = "Hola!"; 
$mensaje2 = "Buen dia!";
$mensaje3 = "Buena tarde!";
$mensaje4 = "Buena Noche";
$mensaje5 = "Cuidado con el coco!";

and the exit outside:

$mensaje1 = "Adios!";
$mensaje2 = "que te vaya bien!";
$mensaje3 = "Cuidado con el perro!";
$mensaje4 = "Ahi espantan!";
$mensaje5 = "Uyy! Qué miedo!";

I hope you can help me, Thanks in advance!

    
asked by Joseph Gonzaga 05.05.2017 в 18:50
source

2 answers

1

To be able to replace the contents, for each message separately. You can do it by passing each message to array and then executing it within a cycle I leave you an example I hope you find it useful:

$array1 = array("Hola!","Buen dia!","Buena tarde!","Buena Noche","Cuidado con el coco!");
    $array2 = array("Adios!","que te vaya bien!","Cuidado con el perro!","Ahi espantan!","Uyy! Qué miedo!");

    for($i = 0; $i< count($array1);$i++){
        echo str_replace($array1[$i], $array1[$i],$array2[$i])."<br />";
    }
  

To show one only at a time

If you want to be shown only one at a time, you just have to place the value of $ i , that is, if you want the zero position of the array You just have to set the value of $ i = 0 and so on for the value you want to show, I'll give you the example:

$array1 = array("Hola!","Buen dia!","Buena tarde!","Buena Noche","Cuidado con el coco!");
$array2 = array("Adios!","que te vaya bien!","Cuidado con el perro!","Ahi espantan!","Uyy! Qué miedo!");
 $i = 0;   
 echo str_replace($array1[$i], $array1[$i],$array2[$i]);
    
answered by 05.05.2017 / 19:42
source
0

As a function and validating that you pass a String to it and that the variable exists, besides passing everything to lowercase to verify better matches, a greeting and good luck !. PS: so for PHP7, if you want it to work in PHP5 remove "string" in the replace function.

<?php
    function remplazar(string $mensaje)
    {
        $array=array(
            "hola"=>"Adios!", 
            "buen dia"=>"Que te vaya bien!",
            "buenas tardes"=>"Cuidado con el perro"
        );
        $mensaje=strtolower($mensaje);
        if( isset($array[$mensaje]) )
        {
            return $array[$mensaje];
        }
        else
        {
            return false;
        }
    }

    //$mensaje="hola";
    $mensaje="HolA";
    if(isset($mensaje) && is_string($mensaje))
    {
        $resultado=remplazar($mensaje);
        if(!$resultado)
        {
            echo "Error no se encuentra mensaje en array";
        }
        else
        {
            echo $resultado;
        }
    }
    else
    {
        echo "Ingrese un mensaje valido de tipo string";
    }
?>
    
answered by 05.05.2017 в 19:56