【问题标题】:PhP extracting striing from HTML with embedded JavaScriptPhP 使用嵌入式 JavaScript 从 HTML 中提取字符串
【发布时间】:2016-08-08 02:11:12
【问题描述】:

我正在尝试从网页中提取此数据 (MARK PATER),我希望它是字符串而不是超链接。这是我的代码:

当我回显时,这是我在浏览器上得到的结果:MARK PATERÂ。我无法将此值提取为字符串...这是一个超链接。当我打开源代码时,我得到了这个:

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

这里是 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>

代码:

$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

【问题讨论】:

  • 这是我查看我想要的字符串时的源代码:MARK PATERÂ string(0) ""

标签: javascript php asp.net html


【解决方案1】:

尝试使用正则表达式

尝试构建正则表达式以从 HTML 中提取字符串。

使用 SimpleXML / DOM 循环浏览 HTML 有时是一个非常令人头疼的过程。

案例样本

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多