Search code examples
phpweb-scrapingsimple-html-dom

How to get text inside javascript tag using PHP Simple HTML DOM Parser


example :

<script>
var TEST = { "contextData" :
{id:01,title:one},{id:02,title:two},{id:03,title:three}}
</script>

how to get all title ? using PHP Simple HTML DOM Parser, I just have no idea to do this.


Solution

  • The following is not tested but should work:

    • Get the script using $script = $html->find('script', 0);
    • Use the following pattern, with preg_match_all as suggested by @pguardiario, to get the titles value: /title\s*:\s*([^}]+)/

    DEMO

    EDIT:

    And to get all titles and desc (different of qwerty) modify the pattern to: /(title|desc)\s*:\s*(?!qwerty)([^},]+)/

    DEMO