How to convert the table content in php into an array
This article mainly shows you "how to convert table content in php into an array", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn how to convert table content in php into an array.
Php table content to array methods: 1, to create a PHP sample file; 2, through the "function tdToArray ($table) {.}" method to convert each row and column of the HTML table into an array.
This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer
Php regular matches the table in the web page html to convert the content into an array
When docking with JD.com vop, when JD.com returns the specification attribute, he returns the style in html format, for example
$date=' main brand good boy main material PP gauge Lattice 800mm*445mm*225'
As for the table above, the preview is
The next step is to extract, and the idea of extraction is to delete the tags such as regular tags first.
Paste the code first
/ / php collects table table data (converts each row of the HTML table to an array) function tdToArray ($table) {$table = preg_replace ("'] *? >'si", ", $table); $table = preg_replace ("'] *? >'si ",", $table); $table = preg_replace ("'] *? >'si", ", $table); $table = str_replace ("," {tr} ", $table) $table = str_replace ("", "{td}", $table); / remove the HTML tag $table = preg_replace ("''si", "", $table); / / remove the white space character $table = preg_replace ("'([rn]) [s] +", ", $table); $table = str_replace (", ", $table); $table = str_replace (",", $table) $table = explode ('{tr}', $table); array_pop ($table); foreach ($table as $key= > $tr) {/ / you can add the corresponding replacement $td = explode ('{td}', $tr); array_pop ($td); $td_array [] = $td;} return $td_array;}
Follow the process with debug, observe each step, without going into details, put a variable of the last $table
Variables after array_pop
It is as follows
The above\ r\ nis to distinguish between line breaks, which can be replaced with str_replace, and the title can also be replaced, as follows
Final effect
The above is all the content of the article "how to convert table content in php into an array". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!