How to implement php regular table replacement: use php on the home page to collect table table data; then use regular rules to delete tags such as "
"; finally add the corresponding replacement That’s it.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php Regularly matches the table in the web page html Convert the content into an array
When connecting to JD.com vop, when JD.com returns the specification attributes, it returns the style in html format, 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>';Copy after loginas above Table, the preview is
The next step is to extract. The extraction idea is to use regular rules to convert
. Wait for the tags to be deleted first. Paste the code first
//php采集table表格数据(将HTML表格的每行每列转为数组) function tdToArray($table) { $table = preg_replace("'<table[^>]*?>'si","",$table); $table = preg_replace("'<tr[^>]*?>'si","",$table); $table = preg_replace("'<td[^>]*?>'si","",$table); $table = str_replace("</tr>","{tr}",$table); $table = str_replace("</td>","{td}",$table); //去掉 HTML 标记 $table = preg_replace("'<[/!]*?[^<>]*?>'si","",$table); //去掉空白字符 $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) { // 自己可添加对应的替换 $td = explode('{td}', $tr); array_pop($td); $td_array[] = $td; } return $td_array; }Copy after loginUse debug to go through the process and observe each step. I will not go into details. I will put a picture of the last $table. Variable
##The variable after array_pop
is as follows
The \r\n above is to distinguish the added line breaks. It can be replaced with str_replace. In addition, the title can also be replaced. As follows
## Final effect
Recommended learning: "
PHP Video TutorialThe above is the detailed content of How to implement regular table replacement in PHP. For more information, please follow other related articles on the PHP Chinese website!
Related labels:source:php.cnPrevious article:What to do if php pictures are written with Chinese garbled characters Next article:How to remove bracket content in php regular expressionStatement of this WebsiteThe 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.cnLatest Articles by Author
2023-04-24 11:00:01 2023-04-24 10:55:51 2023-04-24 10:52:44 2023-04-23 17:40:51 2023-04-23 17:38:02 2023-04-23 17:34:02 2023-04-23 10:15:45 2023-04-23 10:10:52 2023-04-21 16:01:59 2023-04-21 15:58:01Latest IssuesGroup MySQL results by ID for looping over I have a table with flight data in mysql. I'm writing a php code that will group and displ...From 2024-04-06 17:27:5601406Related TopicsMore>Popular RecommendationsPopular TutorialsMore>
JAVA Beginner's Video Tutorial2494606 Latest DownloadsMore>