1

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.

3
  • Have you tried json_decode? Commented Apr 2, 2014 at 9:17
  • 1
    That's not really json. It's not html either, which means... use regex. Commented Apr 2, 2014 at 9:48
  • Would you give an example of its ? Commented Apr 2, 2014 at 12:18

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

@Enisay: thanks it's work. how if this example {id:01,title:one,desc:abc},{id:02,title:two,desc:cde},{id:03,title:three,desc:efg}} i want result all title and desc ?
wow thanks :) one example again, hehe {id:01,title:one,desc:qwerty},{id:02,title:two,desc:cde},{id:03,title:three,desc:efg}} i want result all title and desc , but not desc "qwerty" ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.