2

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"
5
  • Is the & encoded as an html entity? Commented Feb 24, 2010 at 4:11
  • These results are simply not correct. The only correct one is #1. Please run your own code. use var_dump(). view html source output. Commented Feb 24, 2010 at 4:27
  • I am going to post the longer version. The problem might not have been in this part of the code. Commented Feb 24, 2010 at 4:39
  • You should look at the original comment and answer. 15&page is 11 characters long. Commented Feb 24, 2010 at 5:18
  • If you're looking at a web page try "View Source" Commented Feb 24, 2010 at 5:24

2 Answers 2

6

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 '&'.

Sign up to request clarification or add additional context in comments.

1 Comment

@erastusnjuki: Your question update seems to confirm this theory. It appears your string has the ampersand encoded as an html entity.
1

I've run the following with PHP5. Can you provide a complete script? What version of PHP are you running?

$page_string = "15&page";

$page_id_array = explode("&",$page_string);
var_dump($page_id_array);
// array has 2 values, 15 and page
$page_id = $page_id_array[0]; 
echo '<br/>'.$page_id.'<br/>'; 

$page_id_array = explode("15",$page_string);
var_dump($page_id_array);
// array has 2 values, <empty> and &page
$page_id = $page_id_array[0]; 
echo '<br/>'.$page_id.'<br/>'; 

$page_id_array = explode("&page",$page_string);
var_dump($page_id_array);
// array has 2 values, 15 and <empty>
$page_id = $page_id_array[0]; 
echo '<br/>'.$page_id.'<br/>'; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.