[Solved] Filter JSON value encapsulated inside HTML DOM using Regex


Code: (Demo)

function extractColumnData($html,$key){
    if(!preg_match("~\.put\('\d+',\s+(.+?)\);~",$html,$out)){
        return "alert yourself of the preg_match failure";
    }
    if(($array=@json_decode($out[1],true))===null && json_last_error()!==JSON_ERROR_NONE){
       return "alert yourself of the json decode failure";
    }
    return array_column(current(array_column(current($array),'options')),$key,'name');  // this assumes the static structure of your json/array data
}

$html=<<<HTML
<script>
HZ.productVariation.Manager.setSpaceId('33503761');
HZ.data.Variations.put('33503761', {"availVar": [{"id": "c", "label": "Color", "options": [{"name": "Chrome", "avail": 1, "stock": 1, "price": "$174.51", "quantity": "52", "imageId": "3eb1230d05775d3c"}, {"name": "SuperSteel", "avail": 1, "stock": 0, "price": "$341.40", "quantity": "0", "imageId": "d0a126f505775d3e"}]}], "curVar": {"c": "SuperSteel"}, "exactMatch": true});
HZ.productVariation.Manager.setSelector(HZ.productVariation.ListSelector);
HZ.productVariation.Manager.setRenderer(HZ.productVariation.ViewSpaceRenderer);
HZ.productVariation.Manager.setHistoryManager(new HZ.productVariation.BrowserHistoryManager("replace"));
$('.variationSelectors').append(HZ.productVariation.Manager.drawSelectors('33503761'));

HZ.productVariation.Manager.initUI();
</script>
HTML;

echo "avail => ";
var_export(extractColumnData($html,'avail'));
echo "\n----\n";
echo "stock => ";
var_export(extractColumnData($html,'stock'));
echo "\n----\n";
echo "price => ";
var_export(extractColumnData($html,'price'));
echo "\n----\n";
echo "quantity => ";
var_export(extractColumnData($html,'quantity'));
echo "\n----\n";
echo "imageId => ";
var_export(extractColumnData($html,'imageId'));

Output:

avail => array (
  'Chrome' => 1,
  'SuperSteel' => 1,
)
----
stock => array (
  'Chrome' => 1,
  'SuperSteel' => 0,
)
----
price => array (
  'Chrome' => '$174.51',
  'SuperSteel' => '$341.40',
)
----
quantity => array (
  'Chrome' => '52',
  'SuperSteel' => '0',
)
----
imageId => array (
  'Chrome' => '3eb1230d05775d3c',
  'SuperSteel' => 'd0a126f505775d3e',
)

8

solved Filter JSON value encapsulated inside HTML DOM using Regex