Home > Backend Development > PHP Tutorial > PHP获取HTML标签

PHP获取HTML标签

WBOY
Release: 2016-06-23 14:01:46
Original
1694 people have browsed it

<html><body><tr id="ASPxGridView1_DXDataRow0" class="dxgvDataRow_Soft_Orange">	<td class="dxgv">53</td>		<td class="dxgv"><a class="dxeHyperlink" href="onest.aspx?stnm=外海&startm=&endtm=" target="_blank">1</a></td>		<td class="dxgv">2</td>		<td class="dxgv">3</td>		<td class="dxgv">4</td>		<td class="dxgv">6</td>		<td class="dxgv">7</td>		<td class="dxgv">8</td>		<td class="dxgv">9</td>		<td class="dxgv" style="border-right-width:0px;">10</td>	</tr>	<tr id="ASPxGridView1_DXDataRow1" class="dxgvDataRow_Soft_Orange">		<td class="dxgv">11</td>		<td class="dxgv"><a class="dxeHyperlink" href="onest.aspx?stnm=j江苏&startm=&endtm=" target="_blank">12</a></td>		<td class="dxgv">13</td>		<td class="dxgv">14</td>		<td class="dxgv">15</td>		<td class="dxgv">16</td>		<td class="dxgv">17</td>		<td class="dxgv">18</td>		<td class="dxgv">19</td>		<td class="dxgv" style="border-right-width:0px;">20</td>	</tr>	<tr id="ASPxGridView1_DXDataRow1" class="dxgvDataRow_Soft_Orange">		<td class="dxgv">21</td>		<td class="dxgv"><a class="dxeHyperlink" href="onest.aspx?stnm=上海&startm=&endtm=" target="_blank">22</a></td>		<td class="dxgv">23</td>		<td class="dxgv">24</td>		<td class="dxgv">25</td>		<td class="dxgv">26</td>		<td class="dxgv">27</td>		<td class="dxgv">28</td>		<td class="dxgv">29</td>		<td class="dxgv" style="border-right-width:0px;">30</td>	</tr></tr></body></html>	
Copy after login


请教如何获得标签中的1~30这些值呢?最好用simple_html_dom来获取?知道的朋友说下哦!如果不是用这个类也可以!


回复讨论(解决方案)

 $s=<<(\d+)()?#isU",$s,$m);print_r($m[1]);
Copy after login

loadHTML($html);$tr = $dom->getElementsByTagName('tr');$str ='';foreach ($tr as $key => $value) {	$value = trim(str_replace(PHP_EOL, '', $value->nodeValue));	$value = preg_replace('/\s\s+/', '|', $value);		$value = substr($value, strrpos($value, '|')+1);	var_dump($value);	$str.=$value;}var_dump($str);?>
Copy after login

$dom  = new  DOMDocument ;$dom->loadHTML($html);$tr = $dom->getElementsByTagName('tr');$str ='';foreach ($tr as $key => $value) {	$value = trim(str_replace(PHP_EOL, '', $value->nodeValue));	$value = preg_replace('/\s\s+/', ',', $value);		$str.=$value.',';}$str = substr($str, 0,-1);var_dump($str);
Copy after login

$s =<<< HTML<html><body><tr id="ASPxGridView1_DXDataRow0" class="dxgvDataRow_Soft_Orange">    <td class="dxgv">53</td>        <td class="dxgv"><a class="dxeHyperlink" href="onest.aspx?stnm=外海&startm=&endtm=" target="_blank">1</a></td>        <td class="dxgv">2</td>        <td class="dxgv">3</td>        <td class="dxgv">4</td>        <td class="dxgv">6</td>        <td class="dxgv">7</td>        <td class="dxgv">8</td>        <td class="dxgv">9</td>        <td class="dxgv" style="border-right-width:0px;">10</td>    </tr>    <tr id="ASPxGridView1_DXDataRow1" class="dxgvDataRow_Soft_Orange">        <td class="dxgv">11</td>        <td class="dxgv"><a class="dxeHyperlink" href="onest.aspx?stnm=j江苏&startm=&endtm=" target="_blank">12</a></td>        <td class="dxgv">13</td>        <td class="dxgv">14</td>        <td class="dxgv">15</td>        <td class="dxgv">16</td>        <td class="dxgv">17</td>        <td class="dxgv">18</td>        <td class="dxgv">19</td>        <td class="dxgv" style="border-right-width:0px;">20</td>    </tr>    <tr id="ASPxGridView1_DXDataRow1" class="dxgvDataRow_Soft_Orange">        <td class="dxgv">21</td>        <td class="dxgv"><a class="dxeHyperlink" href="onest.aspx?stnm=上海&startm=&endtm=" target="_blank">22</a></td>        <td class="dxgv">23</td>        <td class="dxgv">24</td>        <td class="dxgv">25</td>        <td class="dxgv">26</td>        <td class="dxgv">27</td>        <td class="dxgv">28</td>        <td class="dxgv">29</td>        <td class="dxgv" style="border-right-width:0px;">30</td>    </tr></tr></body></html>HTML;include 'simple_html_dom.php';$p = str_get_html($s);foreach($p->find('td') as $i=>$r) {  if($i) echo $r->text(), PHP_EOL;}
Copy after login
Copy after login
1
2
3
4
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

$s =<<< HTML<html><body><tr id="ASPxGridView1_DXDataRow0" class="dxgvDataRow_Soft_Orange">    <td class="dxgv">53</td>        <td class="dxgv"><a class="dxeHyperlink" href="onest.aspx?stnm=外海&startm=&endtm=" target="_blank">1</a></td>        <td class="dxgv">2</td>        <td class="dxgv">3</td>        <td class="dxgv">4</td>        <td class="dxgv">6</td>        <td class="dxgv">7</td>        <td class="dxgv">8</td>        <td class="dxgv">9</td>        <td class="dxgv" style="border-right-width:0px;">10</td>    </tr>    <tr id="ASPxGridView1_DXDataRow1" class="dxgvDataRow_Soft_Orange">        <td class="dxgv">11</td>        <td class="dxgv"><a class="dxeHyperlink" href="onest.aspx?stnm=j江苏&startm=&endtm=" target="_blank">12</a></td>        <td class="dxgv">13</td>        <td class="dxgv">14</td>        <td class="dxgv">15</td>        <td class="dxgv">16</td>        <td class="dxgv">17</td>        <td class="dxgv">18</td>        <td class="dxgv">19</td>        <td class="dxgv" style="border-right-width:0px;">20</td>    </tr>    <tr id="ASPxGridView1_DXDataRow1" class="dxgvDataRow_Soft_Orange">        <td class="dxgv">21</td>        <td class="dxgv"><a class="dxeHyperlink" href="onest.aspx?stnm=上海&startm=&endtm=" target="_blank">22</a></td>        <td class="dxgv">23</td>        <td class="dxgv">24</td>        <td class="dxgv">25</td>        <td class="dxgv">26</td>        <td class="dxgv">27</td>        <td class="dxgv">28</td>        <td class="dxgv">29</td>        <td class="dxgv" style="border-right-width:0px;">30</td>    </tr></tr></body></html>HTML;include 'simple_html_dom.php';$p = str_get_html($s);foreach($p->find('td') as $i=>$r) {  if($i) echo $r->text(), PHP_EOL;}
Copy after login
Copy after login
1
2
3
4
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
非常感谢LZ!

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