Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3627552
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:54:11+00:00 2026-05-18T23:54:11+00:00

I’m trying to parse some html with PhpQuery, but it is not easy for

  • 0

I’m trying to parse some html with PhpQuery, but it is not easy for me…

I need to extract only the URL(href tag) to an array but it is not working.

Please see this code for example purposes only:

$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">&nbsp;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">&nbsp; 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">&nbsp;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">&nbsp;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">&nbsp;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">&nbsp; 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">&nbsp;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">&nbsp;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">&nbsp;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">&nbsp; ( 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">&nbsp;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">&nbsp;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">&nbsp;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">&nbsp; 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">&nbsp;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">&nbsp;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">&nbsp;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">&nbsp; 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">&nbsp;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">&nbsp;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);

If I echo out the code bellow it get’s me only the href url, and it is ok:

echo $doc['div table a']->attr('href');

If I run the foreach statement I got an array with some null values:

foreach ($doc['div table a'] as $a) {
    $hrefs[] .= pq($a)->attr('href');
}
print_r ($hrefs);

The array I got is:

Array ( 
    [0] => /1153414/ 
    [1] => 
    [2] => /1153399/ 
    [3] => 
    [4] => /1153280/ 
    [5] => 
    [6] => /1152665/ 
    [7] => 
    [8] => /1153524/ 
    [9] => 
    ) 

How can I generate an array like this:

Array ( 
    [0] => /1153414/ 
    [1] => /1153399/ 
    [2] => /1153280/ 
    [3] => /1152665/ 
    [4] => /1153524/ 
    ) 

If you can give me some clues, I would be appreciated.

Sorry my bad english

Best Regards,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T23:54:11+00:00Added an answer on May 18, 2026 at 11:54 pm

    You have five instances of <a/> in your code. This creates an empty a element, rather than closing an existing one. Remove them and your code should work fine.


    Edit A very simple way of removing empty values from an array is running array_filter with no second argument:

    $hrefs = array_filter($hrefs);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.