php - search and delete words from a session variable

0

// I would like to search and delete certain words of a session variable. // In this case the variable contains a query that is generated in another PHP file. // For this I want to eliminate the empty AND. // The query is the following: $_SESSION["consulta_temporal"] = "SELECT * FROM usuarios WHERE (id='user1') AND () AND (nombre='usuario1'";

<?php
session_start();
$consulta = $_SESSION["consulta_temporal"]; // aqui mando a traer la variable de sesion de otr

if (strpos($consulta, '() AND') !== false) {
$resultado1 = substr($consulta, 6);
}
else{
$resultado1 = $consulta ;
}

echo $resultado1;
?>
    
asked by Julian Martinez 03.07.2018 в 21:19
source

1 answer

0

You can do it with explode , convert it to array and then re-assemble the string .

$array = explode("AND ()",$consulta);
$string = implode(" ", $array);

That said, in a session variable it would be more correct to pass the values of the fields you need for the query and mount the query in the php file, not to have it already mounted.

    
answered by 03.07.2018 / 21:37
source