Php preg_match从html中查找json数据
文本htmlPhp preg_match从html中查找json数据,php,Php,文本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|(.*?)"/', $
,["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";
}
试试这个,我希望它能对你有所帮助。如果url在两个delimeters之间,那么这个解决方案是完美的
$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;
}
实际上,解析这种输出不是正确的做法。您需要以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";
}
}
}
如果输入的JSON格式正确,为什么不正确解析JSON数据?没有JSON数据源,它只是来自文件的html\u get\u content如果是正确的html,为什么不解析它?如何解析?我使用json_decode($str,true),但是空白的json_decode
只能用于有效的json结构。如果您的源数据是HTML格式,则类似于DOMDocument
的内容将有所帮助。但是给出的示例看起来不像是有效的JSON或HTML。。。