Reinoud Posted April 17, 2020 Share Posted April 17, 2020 I have this little program: <!DOCTYPE html> <html> <body> <?php echo "why can not I explode array like 0->1, 1->2, 2->3 ..".'<br>'; ?> </body> </html> The output: why can not I explode array like 0->1, 1->2, 2->3 ..input script var total = [1,2,3,4,5];what I got with \ document.write(total) \ :1,2,3,4,5exploded array var_dump: array(1) { [0]=> string(40) "1,2,3,4,5" } so why does not explode the string in an array? This says it all! Link to comment Share on other sites More sharing options...
ckrudelux Posted April 17, 2020 Share Posted April 17, 2020 Say what? php requires a dilimiter then exploding if you have a long.string you will need to extract the data portion before exploding the string into an array $txt = 'A long string 1,2,3,4,5'; explode(',', $txt); outputs an array like this: ['A long string 1', '2', '3', '4', '5'] You can use functions like substr to extract the string protion you need before you use explode to remove unwanted text. Link to comment Share on other sites More sharing options...
Reinoud Posted April 17, 2020 Author Share Posted April 17, 2020 (edited) OK, but what part of $string = 1,2,3,4,5] is not wanted? In your $txt I shpuld remove 'A long string 'right? I see that the little program did not copy right here is teh version: <!DOCTYPE html> <html> <body> <?php echo "why can not I explode array like 0->1, 1->2, 2->3 ..".'<br>'; ?> <script> var a= [1,2,3,4,5] </script> <?php $a= "<script> document.write(a) </script>"; var_dump ($a); $a_exploded= explode(', ', $a); echo '<br> $a_exploded=>'; var_dump ($a_exploded); ?> </body> </html> Edited April 17, 2020 by Reinoud example code was missing Link to comment Share on other sites More sharing options...
Reinoud Posted April 17, 2020 Author Share Posted April 17, 2020 output : why can not I explode array like 0->1, 1->2, 2->3 ..string(36) "1,2,3,4,5"$a_exploded=>array(1) { [0]=> string(36) "1,2,3,4,5" } Link to comment Share on other sites More sharing options...
ckrudelux Posted April 17, 2020 Share Posted April 17, 2020 Yeah, since in your example you have a string witch both have text content and data content. You will have to seperate the two part some how. Either by having them in two different variables, remove parts of the string or to split it. There are several ways to do this. $txt = 'A long string'; $datastr = '1,2,3,4,5'; echo $txt . $datastr; explode(',',$datastr); Link to comment Share on other sites More sharing options...
Reinoud Posted April 17, 2020 Author Share Posted April 17, 2020 and I have "1,2,3,4,5" , what part should be removed? I do not see any longstrings. Or do you, are they hidden or something. Link to comment Share on other sites More sharing options...
Reinoud Posted April 17, 2020 Author Share Posted April 17, 2020 and I have "1,2,3,4,5" , what part should be removed? I do not see any longstrings. Or do you, are they hidden or something. I see now what the problem is: the php variable does not contain the string but the function returning the string! How do I get this value in php? Should it be posted? Link to comment Share on other sites More sharing options...
Ingolme Posted April 17, 2020 Share Posted April 17, 2020 PHP cannot read data from Javasrcript. PHP runs first on the server, then it generates Javascript code and that Javascript code runs after the PHP has already finished. If you want to send data from a Javascript program to PHP, you have to use AJAX. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now