Please help me out on this explode() function issue. I am getting unexpected results for the third scenario, what’s the explanation?
EDIT: The value $page_string is actually from the database. This time I did the tests using var_dump instead of echo. The strings, How does php count them? How is “15&page” count 11?
var_dump($page_string);//string(11) "15&page"
1. Pop out ampersand … fine.
$page_id_array = explode("&",$page_string);
$page_id = $page_id_array[0];
var_dump($page_id);
// string(2) "15"
2. Pop out number … fine
$page_id_array = explode("15",$page_string);
$page_id = $page_id_array[1];
var_dump($page_id);
//string(9) "&blog"
3. Pop out ‘&page’ … why?
$page_id_array = explode("&page",$page_string);
$page_id = $page_id_array[0];
var_dump($page_id);
//string(11) "15&page"
var_dump($page_id_array[1]);
//NULL
EDIT: After answer and comments from jasonbar I did the test which confirms his answer:
$page_id_array = explode("&page",$page_string);
$page_id = $page_id_array[0];
var_dump($page_id);
//string(2) "15"
I think my comment seems a likely explanation. I tried out your code using “15&page” as the test string and it explodes properly using ‘&page’ as the delimiter.
Make sure the & isn’t actually encoded as &
You could try print_r’ing the array you get back from explode() to see if thats really the case. If so you would end up with 15, amp;page when exploding on just ‘&’.