0

From an html content like below, I am trying to get with a php script the content of the javascript array variable "aDataSet". I believe I could use a regular expression but so far I haven't been able to export the data.

What would be the best way to do that? The html content is loaded in a php variable. Ideally I would like to get the information in a php array?

Any idea?

Thank you

<html>
    <head>
    </head>
    <body>
        <!-- html code -->
        <script type="text/javascript">
            var imgShowBtn = '<img src="templates/medias/search.png" title="See form" />';
            var imgEditBtn = '<img src="templates/medias/update.png" title="Modify the complaint" />';
            var imgPdfBtn = '<img src="templates/medias/pdf.jpg" title="See report" />';

            jQuery.fn.dataTableExt.oSort['date-case-asc'] = TriDateFrAsc;
            jQuery.fn.dataTableExt.oSort['date-case-desc'] = TriDateFrDesc;

            var aDataSet = [
            ["   ","1202 26a", "02/29/2012", "01/17/2012", "akdslj91", "345910K30", "Berlin 800905", "4" ],
            ["   ","1202 152", "02/29/2012", "01/17/2012", "ahkqos12", "134711223", "qwerty 5493", "4" ],
            ["   ","1202 241", "02/29/2012", "01/13/2012", "ahkqos12", "345910130", "azerty 1020090951", "4" ],
            ["   ","1202 222", "02/29/2012", "01/31/2012", "askj192a", "136411208", "paris 1020090520", "4" ] ];
        </script>
        <!-- html code -->
    </body>
</html>
Terry
  • 63,248
  • 15
  • 96
  • 118
Terry
  • 21
  • 1
  • 2
    This is probably not the right way to extract variables. Instead, please consider using AJAX. Also, always remember that JS is a client side language, and PHP server side. That is of course if you intend to parse the HTML as text, and not as a rendered web page. – Terry Aug 26 '14 at 22:03
  • Have a look [here](http://stackoverflow.com/questions/5035547/pass-javascript-array-php). And `AJAX` is your friend! You cannot `regex` with `PHP` content that is output. – loveNoHate Aug 26 '14 at 22:03
  • People suggesting AJAX, the OP is running PHP, how would they use AJAX? – Ruan Mendes Aug 26 '14 at 22:18
  • The correct way would be to use a [DOM parser](http://php.net/manual/en/book.dom.php) to get the contents of the script tag, and then a [JavaScript engine](https://github.com/preillyme/v8js) to execute the script. – Ruan Mendes Aug 26 '14 at 22:20

1 Answers1

0

Your use case is not wholly detailed but maybe you could also use the PHPs json_decode function which takes and string representing a Javscript object:

Example

<?php
$strDataset = '[ [" ","1202 26a", "02/29/2012", "01/17/2012", "akdslj91", "345910K30", "Berlin 800905", "4" ], [" ","1202 152", "02/29/2012", "01/17/2012", "ahkqos12", "134711223", "qwerty 5493", "4" ], [" ","1202 241", "02/29/2012", "01/13/2012", "ahkqos12", "345910130", "azerty 1020090951", "4" ], [" ","1202 222", "02/29/2012", "01/31/2012", "askj192a", "136411208", "paris 1020090520", "4" ] ]';
var_dump(json_decode($strDataset));

Code with output available at http://ideone.com/ehzZIY

ivansabik
  • 585
  • 4
  • 13