我需要使用从 HTML 文档中提取的数据来获取 JSON
I need to obtain JSON with data extracted from an HTML document
我正在尝试将包含图像、名称和 phone 编号的公司 phone 列表制作成 JSON 文件。我试图遍历所有 <a>
以找到 img src
和 div.employee-desc
文本,但没有成功。我试过 DOMdocument()
,但也失败了。
<section>
<a href="tel:+471234567890">
<article class="clearfix">
<div class="employee-image">
<img src"image_1.jpg">
</div>
<div class="employee-desc">
Emma doe <br>
+471234567890
</div>
</article>
</a>
<a href="tel:+471234567890">
<article class="clearfix">
<div class="employee-image">
<img src"image_2.jpg">
</div>
<div class="employee-desc">
Frank doe <br>
+471234567890
</div>
</article>
</a>
<a href="tel:+xxxxxxxx">
<article class="clearfix">
<div class="employee-image">
<img src"image_3.jpg">
</div>
<div class="employee-desc">
John doe <br>
+471234567890
</div>
</article></a>
</section>
我的梦想是 json 文件看起来像这样:
[
{
"image":"image_1.jpg",
"name":"Emma doe",
"phone":"+47 1234567890"
},
{
"image":"image_2.jpg",
"name":"Frank doe",
"phone":"+47 1234567890"
},
{
"image":"image_3.jpg",
"name":"John doe",
"phone":"+47 1234567890"
}
]
有人知道如何在 php 中完成这项工作吗?
您可以在下面找到代码。
请注意,您的示例中的 img 标签不正确。应该是 'img src=""' 而不是 'img src""'
我假设你的 html 在 $html 变量中。
$json_arr = array();
$html = substr($html, strpos($html, '<section>') + 9);
$html = substr($html, 0, strpos($html, '</section>'));
$arr = explode('<a href="', $html);
foreach ($arr as $k => $line) {
if ($k == 0) continue;
$phone = substr($line, 0, strpos($line, '"'));
$phone = str_replace('tel:', '', $phone);
$phone = trim($phone);
$image = substr($line, strpos($line, '<img src="') + 10);
$image = substr($image, 0, strpos($image, '"'));
$name = substr($line, strpos($line, '<div class="employee-desc">') + 37);
$name = substr($name, 0, strpos($name, '</div>'));
$name = trim($name);
$name = substr($name, 0, strpos($name, '<br'));
$json_arr[$k - 1]['image'] = $image;
$json_arr[$k - 1]['name'] = $name;
$json_arr[$k - 1]['phone'] = $phone;
}
$json = json_encode($json_arr);
echo $json . "\n";
借助 PHP Simple HTML DOM Parser 的更短方法:
$html = HtmlDomParser::str_get_html($data);
foreach($html->find('a') as $element) {
$image=$element->children(0)->children(0)->children(0)->src;
list($name,$phone)=array_map('trim', explode('<br>',$element->children(0)->children(1)->innertext));
$row = (object)compact('image','name','phone');
$result[]=$row;
}
$output=json_encode($result,JSON_PRETTY_PRINT);
我正在尝试将包含图像、名称和 phone 编号的公司 phone 列表制作成 JSON 文件。我试图遍历所有 <a>
以找到 img src
和 div.employee-desc
文本,但没有成功。我试过 DOMdocument()
,但也失败了。
<section>
<a href="tel:+471234567890">
<article class="clearfix">
<div class="employee-image">
<img src"image_1.jpg">
</div>
<div class="employee-desc">
Emma doe <br>
+471234567890
</div>
</article>
</a>
<a href="tel:+471234567890">
<article class="clearfix">
<div class="employee-image">
<img src"image_2.jpg">
</div>
<div class="employee-desc">
Frank doe <br>
+471234567890
</div>
</article>
</a>
<a href="tel:+xxxxxxxx">
<article class="clearfix">
<div class="employee-image">
<img src"image_3.jpg">
</div>
<div class="employee-desc">
John doe <br>
+471234567890
</div>
</article></a>
</section>
我的梦想是 json 文件看起来像这样:
[
{
"image":"image_1.jpg",
"name":"Emma doe",
"phone":"+47 1234567890"
},
{
"image":"image_2.jpg",
"name":"Frank doe",
"phone":"+47 1234567890"
},
{
"image":"image_3.jpg",
"name":"John doe",
"phone":"+47 1234567890"
}
]
有人知道如何在 php 中完成这项工作吗?
您可以在下面找到代码。 请注意,您的示例中的 img 标签不正确。应该是 'img src=""' 而不是 'img src""'
我假设你的 html 在 $html 变量中。
$json_arr = array();
$html = substr($html, strpos($html, '<section>') + 9);
$html = substr($html, 0, strpos($html, '</section>'));
$arr = explode('<a href="', $html);
foreach ($arr as $k => $line) {
if ($k == 0) continue;
$phone = substr($line, 0, strpos($line, '"'));
$phone = str_replace('tel:', '', $phone);
$phone = trim($phone);
$image = substr($line, strpos($line, '<img src="') + 10);
$image = substr($image, 0, strpos($image, '"'));
$name = substr($line, strpos($line, '<div class="employee-desc">') + 37);
$name = substr($name, 0, strpos($name, '</div>'));
$name = trim($name);
$name = substr($name, 0, strpos($name, '<br'));
$json_arr[$k - 1]['image'] = $image;
$json_arr[$k - 1]['name'] = $name;
$json_arr[$k - 1]['phone'] = $phone;
}
$json = json_encode($json_arr);
echo $json . "\n";
借助 PHP Simple HTML DOM Parser 的更短方法:
$html = HtmlDomParser::str_get_html($data);
foreach($html->find('a') as $element) {
$image=$element->children(0)->children(0)->children(0)->src;
list($name,$phone)=array_map('trim', explode('<br>',$element->children(0)->children(1)->innertext));
$row = (object)compact('image','name','phone');
$result[]=$row;
}
$output=json_encode($result,JSON_PRETTY_PRINT);