Why doesn’t the array value 1 appear in the explode?

Asked

Viewed 68 times

4

When I create the code:

$urlAtual = "https://www.meusite.com.br/customer/account/create/";
$parteurl = explode('/', $urlAtual);
    for($i=0;$i<=6;$i++){
        $parteurldesejada = $parteurl[$i] . "<br>";
        echo $parteurldesejada;
    }

It prints in the browser this:

https:

www.meusite.com.br
customer
account
create

See that the value [1] array, not shown. Why?

Source code looks like this:

<!DOCTYPE html>
<html>
   <head>
      <title>Lucas Carvalho</title>
      <meta charset="UTF-8">
   </head>
   <body>
    https:<br><br>www.meusite.com.br<br>customer<br>account<br>create<br><br></body>
</html>

2 answers

8


It’s because you’re giving explode for / and in the // he recognizes that between them there is an empty value.

Apparently, what you need to do can be done with the function parse_url()

$urlAtual = "https://www.meusite.com.br/customer/account/create/";
var_dump(parse_url($urlAtual));
  • Got it, I’d have to do how then? Could you tell me? Other than by /

  • It depends on what the purpose is. What you want to do?

  • I need to get EVERYONE on the case. Up to the // there.

  • Make an if and print a // when it is in position 1 of the vector.

  • 1

    Just use the parse_url which is a function]suitable for this. @Lucascarvalho

  • I’ll search Daniel, thank you!

  • I edited in his reply instead of creating a new answer.

  • Anyway the parse_url will not print the // which is what you want.

  • I believe that the // is indifferent.. once picking up the parts correctly, which is done in parse_url()

  • But that way he’ll have to give one explode in the path to get what he apparently wants.

Show 5 more comments

3

In itself php.net has an example similar to what you do. It recognizes a / after the / and prints empty

Browser other questions tagged

You are not signed in. Login or sign up in order to post.