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 9076195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:03:21+00:00 2026-06-16T19:03:21+00:00

How do I turn the output into a variable so i can cross reference

  • 0

How do I turn the output into a variable so i can cross reference it to see if it matches another variable I have set

foreach ($nodes as $i => $node) {
  echo $node->nodeValue;
}

I know this is incorrect and wouldn’t work but:

foreach ($nodes as $i => $node) {
  $target = $node->nodeValue;
}

$match = "some text"

if($target == $match) {
  // Match - Do Something
} else {
  // No Match - Do Nothing
}

Actually this solves my question but maybe not the right way about it:

libxml_use_internal_errors(true);
$dom = new DomDocument;
$dom->loadHTMLFile("http://www.example.com");
$xpath = new DomXPath($dom);
$nodes = $xpath->query("(//tr/td/a/span[@class='newprodtext' and contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'adidas')])[1]");
foreach ($nodes as $i => $node) {

echo $node->nodeValue, "\n";
$target[0] = $node->nodeValue;
}

$match = "adidas";

if($target == $match) {
    // Match
} else {
    // No Match
}
  • 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-06-16T19:03:22+00:00Added an answer on June 16, 2026 at 7:03 pm

    Your problem is more about general understanding of loops, assigning values to arrays and using if conditionals with php than using xpath.

    • In your foreach loop, you’re assigning each $node‘s nodeValue to the same index in your $target array, $target will always have only one value (the last one)
    • In your if conditional statement, you’re comparing an array (or null if $nodes has no items, so you probably want to declare $target first) against the string ‘adidas’, that will never be true.

    You probably want to do something like:

    $matched = false;
    $match = 'adidas';
    foreach ($nodes as $i => $node) {
        $nodeValue = trim(strip_tags($node->textContent));
        if ($nodeValue === $match) {
            $matched = true;
            break;
        }
    }
    
    if ($matched) {
        // Match
    } else {
        // No Match
    }
    

    Update

    I see that this xpath expression was given to you in another answer, that presumably already does the matching, so you just need to check the length property of $nodes

    if ($nodes->length > 0) {
        // Match
    } else {
        // No match
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I turn an array of elements into uppercase? Expected output would be:
how can I turn this code into a function with the same output? declare
I want to turn Model.attribute into Attribute.models. e.g. I currently have Model.first.attribute => string
I'm trying to turn this XML string into a select I have @Schedule XML
I need a way to turn code stored in a variable into real code.
According to the feedparser documentation , I can turn an RSS feed into a
I have the below HTML string, and I would like to turn it into
If I turn off Enable Request Debugging Output, the 3rd option under the Debug
I'm trying to turn my data into a burndown chart to monitor the progress
let's say i have the following: $vars=name=david&age=26&sport=soccer&birth=1984; I want to turn this into real

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.