1

I am trying to retrieve a variable (vin=5J8TC2H69KL805815) inside a script tag with a xpath query for practice. The script looks like this:

<script type="text/javascript">
        var enterprise_id = 6731892;
        var dealertrackSettings = {"LowestPaymentWidgetParameters":"prtnr=EDLR&pdid=&jrr=9Z8758kQMpewxBhu8abpaBXX&lang=en&zip=T4R2N7&vin=5J8TC2H69KL805815&acode=CAC90ACS121C0&yr=2019&mk=Acura&model=RDX&style=SUV&trim=A-Spec&cond=U&kms=14,709&cp=52365&msrp=52365&hash=f8842e17a06af7c979d16db0980afbec28c808617e80db2260364c37235b9b25&pkg=&disp=2.0L&opt=&trans=AT&drv=All Wheel Drive&fuelType=Gasoline&eng=2.0L 4cyl&mcode=","DigitalRetailingWidgetParameters":"?flow=full&token=df387c15-9b3d-4694-b06f-fccdfbc8a437&lang=en&prtnr=EDLR&pdid=&zip=T4R2N7&vin=5J8TC2H69KL805815&msrp=52365&cp=52365&asst=AU&cond=U&yr=2019&mk=Acura&model=RDX&style=SUV&trim=A-Spec&tab=TRADE&jrr=9Z8758kQMpewxBhu8abpaBXX&hash=f8842e17a06af7c979d16db0980afbec28c808617e80db2260364c37235b9b25&kms=14,709&acode=CAC90ACS121C0&pkg=&disp=2.0L&opt=&trans=AT&drv=All Wheel Drive&fuelType=Gasoline&eng=2.0L 4cyl&mcode=","LowestPaymentWidgetAPIJson":null,"useUAT":false,"Domain":"https:\/\/digital.dealertrack.ca","displayAFFCTAUsed":true,"displayStructureMyDealCTAUsed":true,"structureMyDealBackgroundUsed":null,"displayFinancePrequalifyCTAUsed":true,"drivetrainMap":{"FRONT WHEEL DRIVE":"FWD","REAR WHEEL DRIVE":"RWD","AWD EXT":"AWD","N\/A":""},"transmissionMap":{"AUTOMATIC":"AT","MANUAL":"MT","OTHER":""}};
    </script>

I narrowed down the text from the script tag like so and this brings back the text.

//script[contains(text(), 'dealertrackSettings')]/text()

but from this a im kinda confused on how to proceed. Any suggestions appreciated. Tahnks

edit: I am trying to do this in php

1 Answer 1

4

Your XPath should be able to get the script content, I've used evaluate() as it is just a string you want. Then I've used a regex() to extract the vin=... part.

$doc = new DOMDocument();
$doc->loadHTML($html);

$xp = new DOMXPath($doc);

$script = $xp->evaluate("string(//script[contains(text(), 'dealertrackSettings')]/text())");
if ( preg_match("/(vin=\w*)/", $script, $matches) ) {
    echo $matches[0];
}

which gives

vin=5J8TC2H69KL805815

If you just want the value, then change the regex to /vin=(\w*)/, this only captures the value and not the vin= part as well.

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

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.