kavali severi kavali severi - 1 year ago 73
ASP.NET (C#) Question

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. Here is my code:

When I echo this is the result that I get on my browser: MARK PATERÂ Â . I am not able to extract this value as a string...It's a 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:

<tr >

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


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



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

Answer Source

Try using regular expression

Try building a Regular Expression to extract strings from HTML.

Looping through HTML using SimpleXML / DOM sometimes is a very head-aching process.

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 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 )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download