6

I'm trying to parse some HTML with Xpath but am finding out the the links I want to get are generated by some javascript and not using just a normal a anchor. The javascript is as follows:

<script type="text/javascript">
    var Hyperurl="ab5";
    var Hyperlink="46439157";
</script>

Now, I've used XPath to grab the script code via:

$xpath->query('//script[contains(.,"Hyper")]');

Which returns:

var Hyperurl="ab5";var Hyperlink="46439157";

My question is. How do I get this data into an array much like parse_url or the like? Should I just preg_match_all the variable storing the string? If so, what regex would I use? Or is there a better way to parse and grab the data I want?

Thanks in advance!

2 Answers 2

3

You could try:

preg_match_all('/"(.*?)"/', $variables, $array);

I think your variables would then be $array[1] and $array[2].

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

1 Comment

Worked like a charm. Thanks a lot. I really need to get better with regex. I always need help with it
2

You could use this

preg_match_all('/var\s+(\w+)\s*=\s*(["\']?)(.*?)\2;/i', $js, $matches);

$matches[1] will contain the variable names, and $matches[3] will contain their values.

Comments

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.