开发者

No elements returned from array

I asked a question about same code but got lost in translation

Here's what the question comes down to. I validate if a certrain var is a an array, then I want an element out of it but returns no value. What's going on?

EDIT: I'm adding the code that explains where the string is coming from. It's a guestbook where I need to come up with a solution that is NOT a database of session variable. It's what my course requires;

   $file="gb_berichten.txt";            
$datum = date("d-m-Y");

$bericht = "<p><b>naam: </b>$_POST[naam] <br>
            <b>email: </b>$_POST[email] <br>
            <b>onderwerp: </b>$_POST[subj]<br><br>
            <b>bericht: </b>$_POST[bericht]<br>
            <b>verzonden op: </b> $datum
            </p>
            <hr/>
            ";

        if (is_writable ($file)){
            $fp= fopen($file, "a")
                or die ("kan de file niet openen<br>");
                fwrite ($fp, $bericht);
                fclose($fp);
            }//end if writable

        else {
            print ("bestand $file is niet beschikbaar voor schrijven<br>");
            }   

        if (is_readable($file)){
            $fp = fopen($file, "r");
            //$tekst = fread($fp, filesize($file));
            $inhoud = file_get_contents($file);
            $berichten =  implode(' ', array_reverse(explode("<hr/>", $inhoud)));
            fclose ($fp); 
            print($berichten);
            }

        else {
            print("niet mogelijk om bestand te lezen");
            } 




    //check of $berichten een string is
    if (is_string($berichten)){
       print ("string ok"); 
    } else开发者_开发技巧{
       print ("string niet ok");} 
       // returns string ok 

       //convert back to array  
       $berichtenArr =  explode("<hr/>", $berichten);
       if (is_array($berichtenArr )) {
          print("<p style='color:red'>Array OK<p>");
       } else {
          print("<p style='color:red'>Array not OK<p>");
       }
       //returns Array OK

       $secondElem = $berichtenArr[1];
       print("<p style='color:red'>second element is: $secondElem<p>"); // returns no value

       $aantalBer = count($berichtenArr);
       print("<p style='color:red'>amount of messages   is: $aantalBer<p>"); //returns 1
    }


You've done explode("<hr/>"), but in one of your comments elsewhere, you state that your string has multiple <hr /> elements.

Can you spot the problem there?

I'll give you a clue: <hr/> is not the same as <hr />.

You probably need to modify your explode() to take both into account.

It's also possible that you might have <hr> as another valid possibility. (In fact, there's a lot more possiblities than that, since is is valid for the <hr> tag to contain id, class and style attributes, all of which will be missed by a simple explode().

If you want to pick up all valid possibilities, you may want to use something like preg_split() instead. (note although I suggest that, I should add that it is generally considered bad form to use a regex to do pattern-matching in HTML. But since you're just looking for the simple <hr> tag, it should be possible to do a good enough job with preg_match() without having to make your matching pattern too complex).


You only have one element in your array. That's why you get no value when you try to get the second element.

Add

print_r($berichtenArr);

and all will be clear.


As per your code your array contains only 1 element so and which is on index 0 .

when you are printing the array value you are referring to index 1.

$secondElem = $berichtenArr[0];

and also check the array structure by printing it print_r($berichtenArr)


I think there's something wrong with your logic: the function explode() takes a string as input and in the previous lines you have made sure that it is not a string, so that code will not work.

Edit: Sorry, I missed the first closing } (there seems to be one too much...) but you still need to wrap your first if statement around all the rest of your code as you cannot continue with your explode if the result of the first if tells you it´s not a string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜