PHP获取HTML标签

WBOY
Lepaskan: 2016-06-23 14:01:46
asal
1664 orang telah melayarinya

<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>	
Salin selepas log masuk


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


回复讨论(解决方案)

 $s=<<(\d+)()?#isU",$s,$m);print_r($m[1]);
Salin selepas log masuk

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);?>
Salin selepas log masuk

$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);
Salin selepas log masuk

$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;}
Salin selepas log masuk
Salin selepas log masuk
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;}
Salin selepas log masuk
Salin selepas log masuk
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!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan