explode and string php [closed]

-5

I want to make an explode as follows:

$variavel = $_GET["url"];

$var1 = explode('<div id="$variavel">',$string);
$var2 = explode('</div>',$var1[1]);

$resultado = $var2[0];

but it is giving error when trying to put the $ variable inside the () of the explode! Can someone help me!

    
asked by anonymous 28.12.2018 / 18:15

3 answers

1

You can use double quotes even without having to concatenate. For when using double quotes inside another it is necessary to put \ before the character "to force the IDE to understand. In your case it looks like this:

With simple quotes you would have to concatenate: < $var1 = explode("<div id=\"$variavel\">",$string);

    
28.12.2018 / 19:34
0

I solved the problem !! Solution:

id="'.$_GET["url"].'"
    
28.12.2018 / 18:40
0

running on ideone

$string='<div id="url">mamae</div>';

$variavel = "url";

$var1 = explode('<div id="'.$variavel.'">',$string);
$var2 = explode('</div>',$var1[1]);

$resultado = $var2[0];

echo $resultado;

See the difference

$variavel = "url";

$var1 = '<div id="$variavel">';
echo $var1;        // vai imprimir <div id="$variavel">

result applying this way

PHP Notice:  Undefined offset: 1 in /home/hJ0i6s/prog.php on line 9

see in

    
28.12.2018 / 19:01