Transform a PHP variable into an array

Asked

Viewed 491 times

2

I have a Javascript variable that is a string "13:00:00", however I would like to pass it to PHP and slice it into an array , separating by ":" but the function of explode does not work with the following code:

<script>

    fin = "02:00:00";

    </script>
</head>
<body>
    <?php
    $fin = "<script>document.write(fin)</script>";       

    $tfin = explode(":", $fin);

    echo "<pre>";        
    var_dump($tfin);
    echo "</pre>";

    ?>


</body>  

How should I proceed to slice this string?

  • why don’t you use the split() js? what is the reason for going to php?

  • Well, I’m developing a game, but they told me to work with timestamp because then I have a ranking and need to sort in relation to the shortest time

  • Use ajax for this or send this value through a form.

  • 1

    From JS pro PHP only has a way if it is with Ajax. Your question is not very clear whether it is PHP->JS or JS->PHP...

  • Besides, because the function explode is not working? Returns an error? What appears there when you make a var_dump?

2 answers

2

PHP is a server language, that is, the script is sent to the server, it is processed and then returns the output, since javascript is a client language, it is not sent to the server to process, so there is no way to capture in this way as you want, unless you use the value already processed via GET, PUT, POST or by parsing the document itself. How you can solve the problem by sending this via AJAX:

  <?php
if(isset($_POST['fin'])) {
$fin = $_POST['fin'];
 $tfin = explode(":", $fin);

    echo "<pre>";        
    var_dump($tfin);
    echo "</pre>";
die();

}
?>

<script src="https://code.jquery.com/jquery-latest.min.js"></script>
  <script>
    $(function(){
       var fin = "02:00:00";
       var data = {fin:fin};
       $.post('<?php echo $_SERVER["SCRIPT_NAME"]; ?>', data, function(e) { 
        document.write('<html><body>'+e+'</body></html>');
       });
    });
 </script>

Another way is to use the explode javascript, the function split():

 <body></body>
      <script>
      var fin = "02:00:00";
        var arrFin = fin.split(":");
        var out = '';
        for (var i in arrFin) {
            out += i + ' <font color="#888a85">=&gt;</font>'+
            ' <small>'+ typeof arrFin[i] +"</small> "+
            "<font color=\"#cc0000\">'" + 
            arrFin[i] + 
            "'</font> <i>(length="+arrFin[i].length+")</i>\n";
        }
        var pre = document.createElement('pre');
        pre.setAttribute('class','xdebug-var-dump');
        pre.setAttribute('dir','ltr');
        pre.innerHTML = out;
        document.body.appendChild(pre);
     </script>

1

As your variable is in Javascript do the replace similarly, take care of the layers of variables.

    <script>
            var fin = "02:00:00";
            res = fin.replace(/\:/g,'.')
            document.getElementById("novotempo").innerHTML = res;
    </script>
<body>
<pre id='novotempo'></pre>
</body>

If you need to pass it on to server-side take a look at AJAX methods.

  • Hi, Dom, greetings are considered noise and are usually removed when some editor will make a upgrade in the post. Just click above the editor’s avatar in "Edited such-time" to see the editing history. Do not doubt that if you have news or problems will ask you here in a comment ;)

  • I didn’t understand the answer, you mentioned split, but used replace.

  • Desulpa was working on another code and I ended up making confusion with what I was writing. Answer edited for future uses.

  • A note @Dom, document.getElementById only works after the element exists, in the order you made it document.getElementById("novotempo") will return null.

  • Sorry, that’s right. I’m working better on my answers here in the stack flow to try to cover all the points.: ) ... Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.