0

The output of the script is: "Outstanding 1... Outstanding 2... Outstanding 3..."

How can I output the for example only the second position of the array, so that I can output the results separately?

Like this: $out[1]

<?php
$html = '
    <div class="page-wrapper">
        <section class="page single-review" itemtype="http://schema.org/Review" itemscope="" itemprop="review">
            <article class="review clearfix">
                <div class="review-content">
                    <div class="review-text" itemprop="reviewBody">
                    Outstanding 1... 
                    </div>
                    <div class="review-text" itemprop="reviewBody">
                    Outstanding 2... 
                    </div>
                </div>
            </article>
        </section>
    </div>
    <div class="review-text" itemprop="reviewBody">
    Outstanding 3... 
    </div>
';

$dom = new DOMDocument;
$dom->loadHTML ($html);
$xpath = new DOMXPath ($dom);
foreach ($xpath->query (".//div[@class='review-text']") as $review)
{
    $out = $review->nodeValue;
    echo $out;
}

?>
1
  • What keeps you from taking the second element of that array and perform whatever you like? Commented Aug 1, 2019 at 14:53

2 Answers 2

1

There are two ways to do this. Either add the entire query result to an array like this

$arr = $xpath->query (".//div[@class='review-text']")

and then call it by

$arr[1]->nodeValue

or if the HTML is big, you can really save memory by doing

    $counter = 0;
    foreach ($xpath->query (".//div[@class='review-text']") as $review)
    {
        if ($counter == 1) {
            $out = $review->nodeValue;
            echo $out;
        }
        $counter++;
    }

Depending on what you need, you can pick one of the options. Also if you need to run other code, but the printing in the foreach, the second way can do that too.

Sign up to request clarification or add additional context in comments.

7 Comments

This is not what I want. I will not Display the $out values in different Lines. I will bring the values to an array with key numbers. So that I can output only Outstanding 2.... or only Outstanding 3...
@Sinalco Sorry, I misunderstood your question. I've edited with changes.
@Sinalco Please accept the answer then, so it can help other people with similar issue.
Sorry Martin, but the last answer from Will is the best answer to help other People.
@Sinalco I don't see how it is better for the general use. He is easier for exactly your case, as you can just copy paste his entire answer, while I give different options that are not 100% for your answer only, while giving explanation, but after all, it is indeed your choice.
|
0

You shoul try something like this

<?php
$html = '
<div class="page-wrapper">
    <section class="page single-review" itemtype="http://schema.org/Review" itemscope="" itemprop="review">
        <article class="review clearfix">
            <div class="review-content">
                <div class="review-text" itemprop="reviewBody">
                    Outstanding 1...
                </div>
                <div class="review-text" itemprop="reviewBody">
                    Outstanding 2...
                </div>
            </div>
        </article>
    </section>
</div>
<div class="review-text" itemprop="reviewBody">
    Outstanding 3...
</div>
';

$dom = new DOMDocument;
$dom->loadHTML ($html);
$xpath = new DOMXPath ($dom);
$reviews = $xpath->query (".//div[@class='review-text']");

echo $reviews[1]->nodeValue; // Outstanding 2...

The point is to store all the reviews in an array and then echo the one you want from the array.

1 Comment

Ooops, I mispelled a variable. Of courses you first added your own $html, yes ? I edit so you have the full example.

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.