简体   繁体   English

PhP使用嵌入式JavaScript从HTML中提取条纹

[英]PhP extracting striing from HTML with embedded JavaScript

I am trying to extract this data (MARK PATER) from the webpage and I want it to be a String and NOT an hyperlink. 我正在尝试从网页中提取此数据(MARK PATER),我希望它是一个字符串而不是一个超链接。 Here is my code: 这是我的代码:

When I echo this is the result that I get on my browser: MARK PATERÂ Â . 当我回声时,这就是我在浏览器上看到的结果:MARKPATERÂ。 I am not able to extract this value as a string...It's a HYPERLINK. 我无法将此值提取为字符串...这是HYPERLINK。 When I open up the source I get this: 当我打开源代码时,我得到以下信息:

<a class="filter_list" href="" onclick="return fillFilterForm(document.formFilter1, 'nation_party_name', 'MARK PATGHL');"><font face="Verdana" size="1" color="BLACK">MARK PATERÂ Â </font></a>string(0) ""

Here is part of the source code from echo $html: 这是echo $ html的部分源代码:

<tr >

<td align="justify" width="5%" nowrap><font face="Verdana" size="1">&nbsp;&nbsp;&nbsp;

*

<a class="list_2" href="details.asp

?doc_id=2&index=0&file_num=07">View</a>&nbsp;&nbsp;</font>

</td>

<td width="20%" align="justify" ><a class="filter_list" href="" onClick="return fillFilterForm(document.formFilter1, 'party_name', 'NEW YORK GORDI’);”><font face="Verdana" size="1" color="BLACK">NEW YORK GORDI&nbsp;&nbsp;</font></td>

<td width="15%" align="justify" nowrap><a class="filter_list" href="" onClick="return fillFilterForm(document.formFilter1, ’Name’, ‘MARK PATER );”><font face="Verdana" size="1" color="BLACK">MARK PATER&nbsp;&nbsp;</font></td>

Code: 码:

$html = file_get_html($link);
//echo htmlspecialchars ($html);
// a new dom object
$dom = new domDocument;  
// load the html into the object
$dom->loadHTML($html); 
$tables = $dom->getElementsByTagName('td');
echo get_inner_html($tables->item(26));


function get_inner_html( $node ) 
{
$innerHTML= '';
$children = $node->childNodes;

foreach ($children as $child)
{
    $innerHTML .= $child->ownerDocument->saveXML( $child );
}

return $innerHTML;

}

enter code here

Try using regular expression 尝试使用正则表达式

Try building a Regular Expression to extract strings from HTML. 尝试构建正则表达式以从HTML提取字符串。

Looping through HTML using SimpleXML / DOM sometimes is a very head-aching process. 使用SimpleXML / DOM遍历HTML有时是一个非常艰巨的过程。

Sample for your case 您的案例样本

$html = "<tr >

<td align=\"justify\" width=\"5%\" nowrap><font face=\"Verdana\" size=\"1\">&nbsp;&nbsp;&nbsp;

*

<a class=\"list_2\" href=\"details.asp?doc_id=2&index=0&file_num=07\">View</a>&nbsp;&nbsp;</font>

</td>

<td width=\"20%\" align=\"justify\" ><a class=\"filter_list\" href=\"\" onClick=\"return fillFilterForm(document.formFilter1, 'party_name', 'NEW YORK GORDI';);\"><font face=\"Verdana\" size=\"1\" color=\"BLACK\">NEW YORK GORDI&nbsp;&nbsp;</font></td>

<td width=\"15%\" align=\"justify\" nowrap><a class=\"filter_list\" href=\"\" onClick=\"return fillFilterForm(document.formFilter1, 'Name', 'MARK PATER';);\"><font face=\"Verdana\" size=\"1\" color=\"BLACK\">MARK PATER&nbsp;&nbsp;</font></td>";

preg_match_all('/(?:<td.+><a.+><font.+>)([\w\s]+)(?:(&nbsp;)+<\/font><\/td>)/', $html, $filtered);

print_r( $filtered[1] );

//Output: Array ( [0] => NEW YORK GORDI [1] => MARK PATER )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM