Is it possible to Ignore Variables in the URL when evaluating them?

1

I want to evaluate the url and what it contains to do different actions (all the urls written are redirected to the index)

but eh arrived at a problem with variables ie

a normal URL would be for example

http://localhost/myapp/productos

and one with Variable could be

http://localhost/myapp/productos/find/2

Sent the url to then respond with a json

The code I have so far

$var = $_GET['url'];
switch ($var) {
case "productos":
 echo "i es igual a 0";
 break;
 //Aqui esta el problema no se como decirle a php que omita el numero que escriba el usuario
            case "productos/find/":
                echo "i es igual a 1";
                break;
            case 2:
                echo "i es igual a 2";
                break;
        }
    
asked by Wilfredo Aleman 27.03.2018 в 16:22
source

2 answers

1

Well at the end I resolved the problem with this code

        $var = $_GET['url']; 
        $numero = intval(preg_replace('/[^0-9]+/', '', $var), 10); 

        switch ($var) {
            case "productos":
                echo "todos los productos";
                break;
            case "productos/find/$numero":
                echo "find";
                break;
            case "productos/familia/$numero":
                echo "i es igual a 2";
                break;
        }

What it does is that it extracts any 10-digit number from a string and then when comparing it, I only add the variable in which the number is that it extracted

    
answered by 27.03.2018 / 19:49
source
1

You can declare the following:

$var = $_GET['url'];
$partes = explode('/', $var);
$to_remove = array_pop($partes);
$sin_id = implode('/', $to_remove);
$comparar = (preg_match('/^\d+$/', $to_remove) !== false) ? $sin_id : $var;
//Ahora puedes hacer tu switch como kerias pero en vez de usar $var, usas $comparar

Finally, what I did was to separate your url in parts and eliminate the last element and assign the variable to compare url if you do not pass a number at the end of the url and I assign it the variable sin_id which is the url formatted without the number.

    
answered by 27.03.2018 в 16:49