Search code examples
mysqlarrayspreg-match-all

inserting multiple notationed preg_match_all element into mysql


I have variables gathered from another website with preg_match_all and echo the value. But the value is in $var1[0][n], n>0 form;I couldn't inserted the value into mysql. Thanks for any help.

$path='@<div class="comp-cell-row-div vtable infoColumn" style="width:    25%;">(.*?)</div>@si';
$vr12=file_get_contents('https://www....');
preg_match_all($path,$vr12,$ad12);
echo $ad12[0][14];

$value = array( var_dump($ad12));

foreach($value as $val){mysqli_query($conn,"UPDATE `test` SET     `param1`='$val' WHERE `id`='118'");
}

 or mysqli_query($conn,"INSERT INTO `test` VALUES .....");

Solution

  • First of all, we need to manipulate data. Choose db as varchar to see saved tags with data. Pregmatc_all also gets tags, strings in varchar format. To eleminate unwanted tags or signs; use trim and substr expressions on php.

    In the example I trim before 17th character. This is a property of strpos

    $data12=trim(substr($ad12[0][n], 17, strpos($ad12[0][n],'')),$extratags);
    mysqli_query($conn,"UPDATE `table` SET `val12` = '$data12' WHERE `col1` = 'data1'; ");