>백엔드 개발 >PHP 튜토리얼 >PHP获取HTML标签

PHP获取HTML标签

WBOY
WBOY원래의
2016-06-23 14:01:461668검색

<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>	


请教如何获得

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

回复讨论(解决方案)

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

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);?>

$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);

$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;}
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;}
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!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.