preg_match_all, regular expression in php with variables

0

Hello, I'm having trouble with the regular expression, and the function returns 3 positions in the array all empty;

preg_match_all('/<a data-role\="sku\" data-sku-id\="'.$the_new_id[2].'\" id\="sku\-3\-'.$the_new_id[2].'\" href\="javascript:void(0)\">(.*)<\a>/i', $page_content, $sku_3_a);

array(3) {
[0]=>
  array(0) {
  }
  [1]=>
  array(0) {
  }
  [2]=>
  array(0) {
  }
    
asked by anonymous 18.03.2018 / 22:34

1 answer

1

It's probably not working because you're escaping the quotes unnecessarily. You just need to escape those quotes, when the function starts with them, for example

preg_match_all("necessário \" escapar", $var)

Otherwise

preg_match_all(' "Aqui" não precisa ', $var)

If you are trying to extract links from a page, simply use the "Regex" below (shorter):

preg_match_all('/<a.*data-sku-id="'.$dataSkuId.'".*id="sku-\d-'.$yourId.'"[^>]*>(.*)<\/a>/i', $page_content, $sku_3_a);

If you just want to get the HTML value, just use strip_tags

strip_tags('<a data-role="sku" data-sku-id="201301155" id="sku-3-201301155" href="javascript:void(0)"><span>32cm</span></a>');

// Output: 32cm
    
19.03.2018 / 00:28