Я пытаюсь разобрать html с помощью PhpQuery, но для меня это нелегко ...
Мне нужно извлечь только массив (тег href) в массив, но он не работает.
Пожалуйста, смотрите этот код только в качестве примера:
$doc = phpQuery::newDocumentHTML('<div align = "left" style="background-color:#FFFFFF;border:1px solid #C3D9FF"> </p>
<table cellPadding="2" cellSpacing="0" width="100%" height="60" style="border-collapse: collapse; ">
<tr>
<td align="left" width="531" height="20"><small>
<strong>
<a href="/1153414/">
<font style="FONT-SIZE: 13px; LINE-HEIGHT: 14px">Industrial</font><a/> </a></small></strong>
</td>
</tr>
<tr>
<td align="left" vAlign="top" width="100%" height="1">
<table align="left" border="0" cellPadding="0" cellSpacing="0" width="736">
<tr>
<td align="left" vAlign="top" width="67">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">
Data:</font></strong></td>
<td align="left" vAlign="top" width="150">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> 4-1-2011 </font></td>
<td align="left" vAlign="top" width="59">
<font color="#000000" face="Arial" size="2">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Zona:</font></strong></td>
<td align="left" vAlign="top" width="473">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> Castelo Branco</font></td>
</tr>
<tr>
<td align="left" vAlign="top" width="67">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Categoria:</font></strong></td>
<td align="left" vAlign="top" width="150">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> Indústria / Produção </font></td>
<td align="left" vAlign="top" width="59">
<font color="#000000" face="Arial" size="2">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Empresa:</font></strong></td>
<td align="left" vAlign="top" width="473">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> Isotransfo, Unipessoal LDA</font></td>
</tr>
</table>
</td>
</tr>
</table>
</p>
<table cellPadding="2" cellSpacing="0" width="100%" height="60" style="border-collapse: collapse; ">
<tr>
<td align="left" width="531" height="20"><small>
<strong>
<a href="/1153399/">
<font style="FONT-SIZE: 13px; LINE-HEIGHT: 14px">Admite-se<a/> </a> </font></small></strong>
</td>
</tr>
<tr>
<td align="left" vAlign="top" width="100%" height="1">
<table align="left" border="0" cellPadding="0" cellSpacing="0" width="736">
<tr>
<td align="left" vAlign="top" width="67">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">
Data:</font></strong></td>
<td align="left" vAlign="top" width="150">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> 4-1-2011 </font></td>
<td align="left" vAlign="top" width="59">
<font color="#000000" face="Arial" size="2">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Zona:</font></strong></td>
<td align="left" vAlign="top" width="473">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> Castelo Branco</font></td>
</tr>
<tr>
<td align="left" vAlign="top" width="67">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Categoria:</font></strong></td>
<td align="left" vAlign="top" width="150">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> Indústria / Produção </font></td>
<td align="left" vAlign="top" width="59">
<font color="#000000" face="Arial" size="2">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Empresa:</font></strong></td>
<td align="left" vAlign="top" width="473">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> Isotransfo, Unipessoal LDA</font></td>
</tr>
</table>
</td>
</tr>
</table>
</p>
<table cellPadding="2" cellSpacing="0" width="100%" height="60" style="border-collapse: collapse; ">
<tr>
<td align="left" width="531" height="20"><small><font face="Arial">
<strong>
<a href="/1153280/">
<font style="FONT-SIZE: 13px; LINE-HEIGHT: 14px">Precisa-se</font><a/> </a> </font></small></strong>
</td>
</tr>
<tr>
<td align="left" vAlign="top" width="100%" height="1">
<table align="left" border="0" cellPadding="0" cellSpacing="0" width="736">
<tr>
<td align="left" vAlign="top" width="67">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">
Data:</font></strong></td>
<td align="left" vAlign="top" width="150">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> 4-1-2011 </font></td>
<td align="left" vAlign="top" width="59">
<font color="#000000" face="Arial" size="2">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Zona:</font></strong></td>
<td align="left" vAlign="top" width="473">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> ( Todas as Zonas )</font></td>
</tr>
<tr>
<td align="left" vAlign="top" width="67">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Categoria:</font></strong></td>
<td align="left" vAlign="top" width="150">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> Saúde / Medicina / Enfermagem </font></td>
<td align="left" vAlign="top" width="59">
<font color="#000000" face="Arial" size="2">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Empresa:</font></strong></td>
<td align="left" vAlign="top" width="473">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> Emprego Radiologia</font></td>
</tr>
</table>
</td>
</tr>
</table>
</p>
<table cellPadding="2" cellSpacing="0" width="100%" height="60" style="border-collapse: collapse; ">
<tr>
<td align="left" width="531" height="20"><small><font face="Arial">
<strong>
<a href="/1152665/">
<font style="FONT-SIZE: 13px; LINE-HEIGHT: 14px">Operadores</font><a/> </a> </font></small></strong>
</td>
</tr>
<tr>
<td align="left" vAlign="top" width="100%" height="1">
<table align="left" border="0" cellPadding="0" cellSpacing="0" width="736">
<tr>
<td align="left" vAlign="top" width="67">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">
Data:</font></strong></td>
<td align="left" vAlign="top" width="150">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> 4-1-2011 </font></td>
<td align="left" vAlign="top" width="59">
<font color="#000000" face="Arial" size="2">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Zona:</font></strong></td>
<td align="left" vAlign="top" width="473">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> Viseu</font></td>
</tr>
<tr>
<td align="left" vAlign="top" width="67">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Categoria:</font></strong></td>
<td align="left" vAlign="top" width="150">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> Lojas / Comércio / Balcão </font></td>
<td align="left" vAlign="top" width="59">
<font color="#000000" face="Arial" size="2">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Empresa:</font></strong></td>
<td align="left" vAlign="top" width="473">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> Dia Portugal Supermercados - Soc. Unip., Lda.</font></td>
</tr>
</table>
</td>
</tr>
</table>
</p>
<table cellPadding="2" cellSpacing="0" width="100%" height="60" style="border-collapse: collapse; ">
<tr>
<td align="left" width="531" height="20"><small><font face="Arial">
<strong>
<a href="/1153524/">
<font style="FONT-SIZE: 13px; LINE-HEIGHT: 14px">Responsável</font><a/> </a> </font></small></strong>
</td>
</tr>
<tr>
<td align="left" vAlign="top" width="100%" height="1">
<table align="left" border="0" cellPadding="0" cellSpacing="0" width="736">
<tr>
<td align="left" vAlign="top" width="67">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">
Data:</font></strong></td>
<td align="left" vAlign="top" width="150">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> 4-1-2011 </font></td>
<td align="left" vAlign="top" width="59">
<font color="#000000" face="Arial" size="2">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Zona:</font></strong></td>
<td align="left" vAlign="top" width="473">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> Santarem</font></td>
</tr>
<tr>
<td align="left" vAlign="top" width="67">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Categoria:</font></strong></td>
<td align="left" vAlign="top" width="150">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> Comercial / Vendas </font></td>
<td align="left" vAlign="top" width="59">
<font color="#000000" face="Arial" size="2">
<strong style="FONT-SIZE: 11px; LINE-HEIGHT: 14px; font-weight:400">
<font face="Arial" color="#333333" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px">Empresa:</font></strong></td>
<td align="left" vAlign="top" width="473">
<font face="Arial" style="FONT-SIZE: 11px; LINE-HEIGHT: 14px"> ALDI Supermercados Lda.</font></td>
</tr>
</table>
</td>
</tr>
</table>
</div>');
//echo $doc['div table a']->attr('href');
foreach ($doc['div table a'] as $a) {
$hrefs[] .= pq($a)->attr('href');
}
print_r ($hrefs);
Если я повторю приведенный ниже код, он получит только URL ссылки, и это нормально:
echo $doc['div table a']->attr('href');
Если я запускаю оператор foreach, я получаю массив с некоторыми нулевыми значениями:
foreach ($doc['div table a'] as $a) {
$hrefs[] .= pq($a)->attr('href');
}
print_r ($hrefs);
Массив, который я получил:
Array (
[0] => /1153414/
[1] =>
[2] => /1153399/
[3] =>
[4] => /1153280/
[5] =>
[6] => /1152665/
[7] =>
[8] => /1153524/
[9] =>
)
Как я могу сгенерировать такой массив, как этот:
Array (
[0] => /1153414/
[1] => /1153399/
[2] => /1153280/
[3] => /1152665/
[4] => /1153524/
)
Если вы можете дать мне несколько подсказок, я был бы признателен.
Извините, мой плохой английский
С наилучшими пожеланиями,