Jump to content
Sign in to follow this  
Reinoud

Getting and using data from js function explode not working

Recommended Posts

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,5
exploded 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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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 by Reinoud
example code was missing

Share this post


Link to post
Share on other sites

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" }

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...