简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM