gpt4 book ai didi

PHP数组到MYSQL数据库?

转载 作者:太空宇宙 更新时间:2023-11-03 12:04:27 24 4
gpt4 key购买 nike

我有一个看起来像这样的表:

<table class="tableclass">
<tr>
<th></th>
<th> Totals/Avg </th>
<th><a href='https://url/id=10607' > Column 1 </a></th>
<th><a href='https://url/id=11658' > Column 2 </a></th>
<th><a href='https://url/id=12562' > Column 3 </a></th>
<th><a href='https://url/id=13278' > Column 4 </a></th>
<th><a href='https://url/id=14696' > Column 5 </a></th>
<th><a href='https://url/id=16255' > Column 6 </a></th>
</tr>
<tr>
<td><b> Row Info 1 </b></td>
<td> 5,400.00 </td>
<td> 900.00 </td>
<td> 900.00 </td>
<td> 900.00 </td>
<td> 900.00 </td>
<td> 900.00 </td>
<td> 900.00 </td>
</tr>
<tr>
<td><b> Row Info 2 </b></td>
<td> 6,000.00 </td>
<td> 1,000.00 </td>
<td> 1,000.00 </td>
<td> 1,000.00 </td>
<td> 1,000.00 </td>
<td> 1,000.00 </td>
<td> 1,000.00 </td>
</tr>
<tr>
<td><b> Row Info 3 </b></td>
<td> 535,547 </td>
<td> 91,186 </td>
<td> 90,148 </td>
<td> 89,694 </td>
<td> 90,900 </td>
<td> 90,662 </td>
<td> 82,957 </td>
</tr>
</table>

使用我在这里找到的信息:How to parse this table and extract data from it?

我能够将数据放入数组中:

Array
(
[0] =>
[1] => Array
(
[] => Row Info 1
[Totals/Avg] => 5,400.00
[Column 1] => 900.00
[Column 2] => 900.00
[Column 3] => 900.00
[Column 4] => 900.00
[Column 5] => 900.00
[Column 6] => 900.00
)

[2] => Array
(
[] => Row Info 2
[Totals/Avg] => 6,000.00
[Column 1] => 1,000.00
[Column 2] => 1,000.00
[Column 3] => 1,000.00
[Column 4] => 1,000.00
[Column 5] => 1,000.00
[Column 6] => 1,000.00
)

[3] => Array
(
[] => Row Info 3
[Totals/Avg] => 535,547
[Column 1] => 91,186
[Column 2] => 90,148
[Column 3] => 89,694
[Column 4] => 90,900
[Column 5] => 90,662
[Column 6] => 82,957
)

)

我需要做的是将数据存储到 mysql 中,用实际数据代替行信息进行布局:

第 1 列 - 行信息 1 - 行信息 2 - 行信息 3

我整晚都在为这个问题苦思冥想,所以我在这里。

编辑:我需要做的基本上是安排数组,以便我可以这样存储它

Array
(
[0] =>
[1] => Array
(
[] => Column 1
[Row Info 1] => data
[Row Info 2] => data
[Row Info 3] => data
)

[2] => Array
(
[] => Column 2
[Row Info 1] => data
[Row Info 2] => data
[Row Info 3] => data
)

[3] => Array
(
[] => Column 3
[Row Info 1] => data
[Row Info 2] => data
[Row Info 3] => data
)
...etc

)

最佳答案

你可以像这样使用正则表达式:

只需将您的 HTML 标记存储在 PHP 变量中:

    $details = '<table class="tableclass"> <tr> <th> </th> <th> Totals/Avg </th> <th> <a href='https://url/id=10607' > Column 1 </a> </th> <th> <a href='https://url/id=11658' > Column 2 </a> </th> <th> <a href='https://url/id=12562' > Column 3 </a> </th> <th> <a href='https://url/id=13278' > Column 4 </a> </th> <th> <a href='https://url/id=14696' > Column 5 </a> </th> <th> <a href='https://url/id=16255' > Column 6 </a> </th> </tr><tr> <td> <b> Row Info 1 </b> </td><td> 5,400.00 </td><td> 900.00 </td><td> 900.00 </td><td> 900.00 </td><td> 900.00 </td><td> 900.00 </td><td> 900.00 </td></tr><tr> <td> <b> Row Info 2 </b> </td><td> 6,000.00 </td><td> 1,000.00 </td><td> 1,000.00 </td><td> 1,000.00 </td><td> 1,000.00 </td><td> 1,000.00 </td><td> 1,000.00 </td></tr><tr> <td> <b> Row Info 3 </b> </td><td> 535,547 </td><td> 91,186 </td><td> 90,148 </td><td> 89,694 </td><td> 90,900 </td><td> 90,662 </td><td> 82,957 </td></tr>';

之后使用 preg_match_all() 作为:

preg_match_all('/info.*?<td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td>.*?<\/tr>/is',$details,$matches);

在结果中,您将找到分别包含行信息 1、2 和 3 值的数组。

您可以使用

检查您的结果
print_r($matches);

现在您可以将您的值存储到数据库中。

谢谢。

关于PHP数组到MYSQL数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27294001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com