我正在尝试将一些html转换为数组,然后转换为json字符串。
我正在根据此参考开发:https://www.codeproject.com/Tips/1074174/Simple-Way-to-Convert-HTML-Table-Data-into-PHP-Arr
这是我想要将其转换为JSON的基本表/ html。
<table class="table-list table table-responsive table-striped">
<thead>
<tr>
<th class="coll-1 name">name</th>
<th class="coll-2">height</th>
<th class="coll-3">weight</th>
<th class="coll-date">date</th>
<th class="coll-4"><span class="info">info</span></th>
<th class="coll-5">country</th>
</tr>
</thead>
<tbody>
<tr>
<td class="coll-1 name">
<a href="/username/Jhon Doe/" class="icon"><i class="flaticon-user"></i></a>
<a href="/username/Jhon Doe/">Jhon Doe</a>
</td>
<td class="coll-2 height">45</td>
<td class="coll-3 weight">50</td>
<td class="coll-date">9am May. 16th</td>
<td class="coll-4 size mob-info">abcd</td>
<td class="coll-5 country"><a href="/country/CA/">CA</a></td>
</tr>
<tr>
<td class="coll-1 name">
<a href="/username/Kasim Shk/" class="icon"><i class="flaticon-user"></i></a>
<a href="/username/Kasim Shk/">Kasim Shk</a>
</td>
<td class="coll-2 height">33</td>
<td class="coll-3 weight">54</td>
<td class="coll-date">Mar. 14th '18</td>
<td class="coll-4 size mob-info">ijkl</td>
<td class="coll-5 country"><a href="/country/UAE/">UAE</a></td>
</tr>
</tbody>
</table>
我想要像这样的json输出:
[
{
"user_link": "/username/Jhon Doe/",
"name": "Jhon Doe",
"height": "45",
"weight": "50",
"date": "Apr. 01st '18",
"info": "abcd",
"country": "CA"
},
{
"user_link": "/username/Kasim Shk/",
"name": "Kasim Shk",
"height": "33",
"weight": "54",
"date": "Mar. 14th '18",
"info": "ijkl",
"country": "UAE"
}
]
这就是我在PHP中尝试的:(
但是,这无法获取用户的链接,并且JSON中的名称不正确。
(HTML表与上面的http://rudraproduction.in/htmltable.php相同)
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
error_reporting(E_ERROR | E_PARSE);
$time_start = microtime(true);
$All = [];
$link = 'http://rudraproduction.in/htmltable.php';
$jsonData = file_get_contents($link);
//echo $jsonData;
$dom = new DOMDocument;
$dom->loadHTML($jsonData);
$tables = $dom->getElementsByTagName('table');
$tr = $dom->getElementsByTagName('tr');
foreach ($tr as $element1) {
for ($i = 0; $i < count($element1); $i++) {
//Not able to fetch the user's link :(
$link = $element1->getElementsByTagName('td')->item(0)->getElementsByTagName('a'); // To fetch user link
$name = $element1->getElementsByTagName('td')->item(0)->textContent; // To fetch name
$height = $element1->getElementsByTagName('td')->item(1)->textContent; // To fetch height
$weight = $element1->getElementsByTagName('td')->item(2)->textContent; // To fetch weight
$date = $element1->getElementsByTagName('td')->item(3)->textContent; // To fetch date
$info = $element1->getElementsByTagName('td')->item(4)->textContent; // To fetch info
$country = $element1->getElementsByTagName('td')->item(5)->textContent; // To fetch country
array_push($All, array(
"user_link" => $link,
"name" => $name,
"height" => $height,
"weight" => $weight,
"date" => $date,
"info" => $info,
"country" => $country
));
}
}
echo json_encode($All, JSON_PRETTY_PRINT);
我的PHP代码的输出是:
[
{
"user_link": "http:\/\/rudraproduction.in\/htmltable.php",
"name": null,
"height": null,
"weight": null,
"date": null,
"info": null,
"country": null
},
{
"user_link": "http:\/\/rudraproduction.in\/htmltable.php",
"name": "\r\n\t\t\r\n\t\tJhon Doe\r\n\t",
"height": "45",
"weight": "50",
"date": "9am May. 16th",
"info": "abcd",
"country": "CA"
},
{
"user_link": "http:\/\/rudraproduction.in\/htmltable.php",
"name": "\r\n\t\t\r\n\t\tKasim Shk\r\n\t",
"height": "33",
"weight": "54",
"date": "Mar. 14th '18",
"info": "ijkl",
"country": "UAE"
}
]
答案 0 :(得分:1)
由于语法的实用性/易用性,我更倾向于将XPath
与DomDocument
一起使用。通过定位<tr>
标记中的<tbody>
元素,您可以访问所有必需的数据。
除href
值外,最终&#34;所有字母&#34;每个<td>
类值中的子字符串表示关联值的所需键。为此,我使用preg_match()
来提取最终的&#34;字&#34;在class属性中。
当$key
为name
时,href
属性值必须与硬编码密钥一起存储:user_link
。
您的样本日期值需要进行一些准备以产生所需的格式。由于输入数据不同,您可能需要修改正则表达式以允许strtotime()
正确处理日期表达式。
代码:(Demo)
$html = <<<HTML
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table class="table-list table table-responsive table-striped" border="1">
<thead>
<tr>
<th class="coll-1 name">name</th>
<th class="coll-2">height</th>
<th class="coll-3">weight</th>
<th class="coll-date">date</th>
<th class="coll-4"><span class="info">info</span></th>
<th class="coll-5">country</th>
</tr>
</thead>
<tbody>
<tr>
<td class="coll-1 name">
<a href="/username/Jhon Doe/" class="icon"><i class="flaticon-user"></i></a>
<a href="/username/Jhon Doe/">Jhon Doe</a>
</td>
<td class="coll-2 height">45</td>
<td class="coll-3 weight">50</td>
<td class="coll-date">9am May. 16th</td>
<td class="coll-4 size mob-info">abcd</td>
<td class="coll-5 country"><a href="/country/CA/">CA</a></td>
</tr>
<tr>
<td class="coll-1 name">
<a href="/username/Kasim Shk/" class="icon"><i class="flaticon-user"></i></a>
<a href="/username/Kasim Shk/">Kasim Shk</a>
</td>
<td class="coll-2 height">33</td>
<td class="coll-3 weight">54</td>
<td class="coll-date">Mar. 14th '18</td>
<td class="coll-4 size mob-info">ijkl</td>
<td class="coll-5 country"><a href="/country/UAE/">UAE</a></td>
</tr>
</tbody>
</table>
</body>
</html>
HTML;
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach ($xpath->query('//tbody/tr') as $tr) {
$tmp = []; // reset the temporary array so previous entries are removed
foreach ($xpath->query("td[@class]", $tr) as $td) {
$key = preg_match('~[a-z]+$~', $td->getAttribute('class'), $out) ? $out[0] : 'no_class';
if ($key === "name") {
$tmp['user_link'] = $xpath->query("a[@class = 'icon']", $td)[0]->getAttribute('href');
}
$tmp[$key] = trim($td->textContent);
}
$tmp['date'] = date("M. dS 'y", strtotime(preg_replace('~\.|\d+[ap]m *~', '', $tmp['date'])));
$result[] = $tmp;
}
var_export($result);
echo "\n----\n";
echo json_encode($result);
输出:(作为multidim数组,然后是json编码的字符串)
array (
0 =>
array (
'user_link' => '/username/Jhon Doe/',
'name' => 'Jhon Doe',
'height' => '45',
'weight' => '50',
'date' => 'May. 16th \'18',
'info' => 'abcd',
'country' => 'CA',
),
1 =>
array (
'user_link' => '/username/Kasim Shk/',
'name' => 'Kasim Shk',
'height' => '33',
'weight' => '54',
'date' => 'Jan. 01st \'70',
'info' => 'ijkl',
'country' => 'UAE',
),
)
----
[{"user_link":"\/username\/Jhon Doe\/","name":"Jhon Doe","height":"45","weight":"50","date":"May. 16th '18","info":"abcd","country":"CA"},{"user_link":"\/username\/Kasim Shk\/","name":"Kasim Shk","height":"33","weight":"54","date":"Jan. 01st '70","info":"ijkl","country":"UAE"}]