0

I have a result from a curl request from a page like this:

$result =

<div class="c-wrapper">
  <a href="link-to-a-page.php">
    <div class="c-content-img">
     <img src="...">
    </div>
    <div class="c-link-data">
     <div class="c-link-data-title">
     <h4>TITLE</h4>
     </div>
    </div>
  </a>
<div>

<div class="c-wrapper">
    <div class="c-content-img">
     <img src="...">
    </div>
    <div class="c-link-data">
     <div class="c-link-data-title">
     <h4>TITLE 2</h4>
     </div>
    </div>
<div>

Now I have to count how many c-wrapper is present:

I use correctly this:

$doc = new DOMDocument();
@$doc->loadHTML($result);
$xpath = new DOMXPath($doc);
$divs = $xpath->query("//div[contains(@class, 'c-wrapper')]");

echo $divs-length; //<--- printed: 2

Then I have to print all titles:

I use correctly this:

$titles = $xpath->query("//div[contains(@class, 'c-link-data-title')]/h4");

foreach ($titles as $title) {
    echo $title->textContent . "<br>";
}

Now the part I don't know: In the first div is present a link, in the second one no link. I'd like to edit my print of titles like this:

foreach ($titles as $title) {
    if ( $link_extracted !="" ) 
    echo "<a href='" . $link_extracted . "'>" . $title->textContent . "</a><br>";
    else
    echo $title->textContent . "<br>";
}

How can I edit $titles = $xpath->query("//div[contains(@class, 'c-link-data-title')]/h4"); to achieve this?

1 Answer 1

1

Rather than doing this in separate stages, the code finds the c-wrapper elements and then further uses XPath to find the various parts you want inside that particular element, so in

$link_extracted = $xpath->evaluate("a/@href", $div)[0];

it is looking for an <a> element relative to the $div element. Using [0] as you want only the first one.

$doc = new DOMDocument();
@$doc->loadHTML($result);
$xpath = new DOMXPath($doc);
$divs = $xpath->query("//div[contains(@class, 'c-wrapper')]");

echo $divs->length;

foreach ( $divs as $div ) {
    $link_extracted = $xpath->evaluate("a/@href", $div)[0];
    $title = $xpath->evaluate("descendant::div[contains(@class, 'c-link-data-title')]/h4/text()"
        , $div)[0];
    if ( !empty($link_extracted->nodeValue) )  {
        echo "<a href='" . $link_extracted->nodeValue . "'>" . $title->textContent . "</a><br>";
    }
    else    {
        echo $title->textContent . "<br>";
    }
}

which for your test HTML gives...

2<a href='link-to-a-page.php'>TITLE</a><br>TITLE 2<br>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.