Home > Backend Development > PHP Problem > How to convert php table content to array

How to convert php table content to array

藏色散人
Release: 2023-03-13 09:20:01
Original
2650 people have browsed it

How to convert php table content to array: 1. Create a PHP sample file; 2. Convert each row and column of the HTML table to an array through the "function tdToArray($table) {...}" method That’s it.

How to convert php table content to array

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to convert the content of php table to an array?

php Regularly matches the table in the html of the web page and converts the content into an array

When connecting to JD.com vop, when JD.com returns the specification attribute, the style in html format is returned. For example

$date='
<table cellpadding="0" cellspacing="1" width="100%"border="0" class="Ptable">
         <tr>
            <th class="tdTitle" colspan="2">主体
           </th>
         <tr>
         <tr>
              <td class="tdTitle">品牌
              </td>
              <td>好孩子
              </td>
         </tr>
         <tr>
             <td class="tdTitle">主材质
             </td>
             <td>PP
             </td>
         </tr>
         <tr>
                <td class="tdTitle">规格
                </td>
                <td>800mm*445mm*225
                </td>
        </tr>
        </table>&#39;;
Copy after login

the above table, the preview is

The next step is to extract, the extraction idea is to use regular expressions to convert

and other tags are deleted first.

Paste the code first

//php采集table表格数据(将HTML表格的每行每列转为数组)
function tdToArray($table) {
    $table = preg_replace("&#39;<table[^>]*?>&#39;si","",$table);
    $table = preg_replace("&#39;<tr[^>]*?>&#39;si","",$table);
    $table = preg_replace("&#39;<td[^>]*?>&#39;si","",$table);
    $table = str_replace("</tr>","{tr}",$table);
    $table = str_replace("</td>","{td}",$table);
    //去掉 HTML 标记
    $table = preg_replace("&#39;<[/!]*?[^<>]*?>&#39;si","",$table);
    //去掉空白字符
    $table = preg_replace("&#39;([rn])[s]+&#39;","",$table);
    $table = str_replace(" ","",$table);
    $table = str_replace(" ","",$table);
    $table = explode(&#39;{tr}&#39;, $table);
    array_pop($table);
    foreach ($table as $key=>$tr) {      // 自己可添加对应的替换
        $td = explode(&#39;{td}&#39;, $tr);
        array_pop($td);
        $td_array[] = $td;
    }
    return $td_array;
}
Copy after login

Use debug to go through the process and observe each step. I won’t go into details, but put a variable of the last $table

The variables after array_pop

are as follows

The \r\n above is to distinguish the added line breaks, which can be replaced by str_replace. In addition, the title can also be replaced, as follows

Final effect

## Recommended learning: "

PHP Video Tutorial

The above is the detailed content of How to convert php table content to array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
,