preg_match从html中查找json数据

时间:2018-05-19 06:28:35

标签: php

text html

,["pid","VJHggI_XrKLWQ"]
,["image_full","1080|https://pic.website.com/bdnsns,800|https://pic2.website.com/bdnsns"]

如何获取字符串https://pic.website.com/bdnsns

我试过这段代码:

$str = file_get_contents($str);

if(preg_match('/"image_full","1080|(.*?)"/', $str, $m)){
  echo  $m[1] . "\n";
}

2 个答案:

答案 0 :(得分:0)

试试这个,我希望它对你有所帮助。如果网址在两个分隔符之间,那么这个解决方案是完美的。

$str = file_get_contents("test.html");
print_r(extract_unit($str,'|',','));
function extract_unit($string, $start, $end)
 {
  $pos = stripos($string, $start);

  $str = substr($string, $pos);

  $str_two = substr($str, strlen($start));

  $second_pos = stripos($str_two, $end);

  $str_three = substr($str_two, 0, $second_pos);

  $unit = trim($str_three); // remove whitespaces

  return $unit;
 }

答案 1 :(得分:0)

实际上,解析这种输出并不是正确的做法。您需要以JSON格式正确获取数据。无论如何你需要它,那么你可以做到这一点。

<?php

$str = ',["pid","VJHggI_XrKLWQ"],["image_full","1080|https://pic.website.com/bdnsns,800|http://pic2.website.com/bdnsns"],["image_full","1080|https://pic.website.com/bdnsns,800|https://pic2.website.com/bdnsns"]';

$m = array();

if(preg_match_all('/\d+\|http(s)?:\/\/.+(\,|\")/U', $str, $m)){
    foreach($m[0] as $each_match){
        $each_match = trim(trim($each_match,","),'"');
        $new_matches = array(); 
        if(preg_match('/http(s)?:\/\/.+/',$each_match,$new_matches)){
            echo $new_matches[0],"\n";
        }
    }
}