/    Sign up×
Community /Pin to ProfileBookmark

PHP’s loadHTML, xpath, and comments

I’m reading an HTML file for modification using xpath, and in doing so I’m trying to read the contents of a javascript script tag so that I can extract some variable names from it. I had it working when I was using loadXML, but I switched over to loadHTML after I ran into problems with it. Before, I had:

[code=php]$query = $xpath->query(“//script/comment()”);
$string = htmlentities($query->item(0)->nodeValue);[/code]

However, after loading my file via loadHTML, this no longer seems to work. The script is encased in <!– //<![CDATA[ and //]]> –>

The entirety of what I’m loading in:

[code=php]
$course_dom = new DOMDocument();
@$course_dom->loadHTML($string);

$xpath = new DOMXPath($course_dom);
$query = $xpath->query(“//script/comment()”);
$string = htmlentities($query->item(0)->nodeValue);

[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@aihazm_yahoo_coSep 22.2009 — maybe youshould remove the @ sign before $course_dom->loadHTML($string); so we can see output errors
Copy linkTweet thisAlerts:
@kepardueauthorSep 23.2009 — Seems to read the document in just fine. There are no problems there with the @ sign. It reads the rest of the tags and values just fine.

The problem is that it's just not returning *anything* within the <script><!-- //<![CDATA[ and //]]> --></script> tags.
Copy linkTweet thisAlerts:
@kepardueauthorOct 01.2009 — Bump.
×

Success!

Help @kepardue spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...