Search code examples
phpgouttedomcrawler

Goutte - get link from a td while iterating through columns


I am trying to get a mixture of text and href from a table, the last column contains href while the others is just text. How can I get the text and appropriate href while iterating.

I have html table and I want to make array from the table

$html = '<table>
<tr>
    <td>user1</td>
    <td>address1</td>
    <td>dob1</td>
    <td>status1</td>
    <td>link1</td>
</tr>
<tr>
    <td>user2</td>
    <td>address2</td>
    <td>dob2</td>
    <td>status2</td>
    <td>link2</td>
</tr>
<tr>
    <td>user3</td>
    <td>address3</td>
    <td>dob3</td>
    <td>status3</td>
    <td>link3</td>
</tr>
</table>

As you can see, the last column is a link while the others are text, I want to extract the text and link so that my array must look like this

 array(
      "user1",
      "address1",
      "dob1",
      "status1",
      "<a href='link1'><img src='profile.jpg' /><a/>",
   ),
 array(
      "user2",
      "address2",
      "dob2",
      "status2",
      "<a href='link2'><img src='profile.jpg' /><a/>",
   ),
 array(
      "user3",
      "address3",
      "dob3",
      "status3",
      "<a href='link3'><img src='profile.jpg' /><a/>",
   )
)

I now can use this function below to get the text from the table

$table = $crawler->filter('table')->filter('tr')->each(function ($tr, $i) {
    return $tr->filter('td')->each(function ($td, $i) {
        return trim($td->text());
    });
});

but the last column is a link how do i capture it using link()?


Solution

  • can you check by $i in second loop? so if $i==3 it means function should return link().