Php 提取表格内容的正则表达式

WBOY
Release: 2016-06-13 12:08:21
Original
1479 people have browsed it

求一个Php 提取表格内容的正则表达式
字符串可能如下:
$s1 = '这里是描述字符( )

AAAAA   BBBBB 
';

$s2= ’这里是描述字符( )
Php 提取表格内容的正则表达式   0
';

希望能把table表格前的字符 :这里是描述字符( )’ 和 td 中间的字符串提取出来
'BBBBB '、'AAAAA'、'Php 提取表格内容的正则表达式'、'0
表格td中间内容可能比较杂,尽量考虑各种情况。


不知道描述得清楚么? 各位正则达人请支招!

------解决思路----------------------
$s1 = '这里是描述字符( )<table name="optionsTable" cellpadding="0" cellspacing="0" width="100%"><tr><td width="25%">AAAAA </td><td width="25%"> BBBBB </td></tr></table>';<br />$s2= '这里是描述字符( )<table name="optionsTable" cellpadding="0" cellspacing="0" width="100%"><tr><td width="50%"><img  src="2061.png"   style="max-width:90%" / alt="Php 提取表格内容的正则表达式" > </td><td width="50%"> <sub>0</sub></td></tr></table>';<br />//匹配table前面的内容<br />preg_match_all('/(.+?)<table[^>]+?>/i',$s1,$p1);<br />echo "<pre class="brush:php;toolbar:false">";<br />print_r($p1[1]);<br />echo "
Copy after login
";
//匹配td里面的内容
preg_match_all('/]+?>(.+?)<\/td>/i',$s2,$m2);
echo "
";<br />print_r($m2[1]);<br />echo "
Copy after login
";

//$p1[1]和$m2[1]就是匹配到的内容
------解决思路----------------------
<br /><?php<br /><br />$s1 = '这里是描述字符( )<table name="optionsTable" cellpadding="0" cellspacing="0" width="100%"><tr><td width="25%">AAAAA </td><td width="25%"> BBBBB </td></tr></table>';<br /><br />$s2 = '这里是描述字符( )<table name = "optionsTable" cellpadding = "0" cellspacing = "0" width = "100%"><tr><td width = "50%"><img  src = "2061.png" style = "vertical-align:middle;" / alt="Php 提取表格内容的正则表达式" > </td><td width = "50%"> <sub>0</sub></td></tr></table>';<br /><br />//换个思路吧,不要提取而是删减<br />//包括table/tr标签就再加2条<br />$replace = array('/<\s*td.*>/U', '/<\s*\/td\s*>/U');<br /><br />echo preg_replace($replace, '', $s1);<br />echo '<br />';<br />echo preg_replace($replace, '', $s2);<br /><br />
Copy after login
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