0

I'm using explode to break a FQDN and store it in an array. Calling pop on that array however returns a blank or empty string, and I can't figure out why.

 $domains = explode(".",str_replace("example.com","","test.example.com"));
 print "Test: " . $domains[0] . "<br>";
 $target = array_pop($domains);
 print "Target: " . $target . "<br>";

Running the above code results in:

 Test: test
 Target: 

Why doesn't $target contain "test"?

rthur
  • 1,466
  • 1
  • 15
  • 15
  • Didn't checked that as you.. But, now its clear, try `var_dump($domains)` to see.. It contains **`2`** elements.. `test` and an empty string. However, this is not a reason for a downvote (imo) – hek2mgl Nov 29 '13 at 22:46

2 Answers2

2

In effect, here's what you're actually doing:

var_dump(explode('.', 'test.'));

array(2) {
  [0]=>
  string(4) "test"
  [1]=>
  string(0) ""
}

You get two elements in the array: "test" and what's after the period i.e. an empty string.

Nev Stokes
  • 9,051
  • 5
  • 42
  • 44
1

array_pop() pops and returns the last value of the array

Use array_shift

Scony
  • 4,068
  • 1
  • 16
  • 23
  • I actually want the last element of the array, it was the whitespace as mentioned by Nev Stokes that threw me off. Thanks for the reply! – rthur Nov 29 '13 at 22:48