0

source code:

<div id="point">9</div>
<div id="point">REAL POINT: 9</div>

and parser code:

$point = $html->find('div[id=point]');

so, when you write $point[0] it will be first, and the other will be second.

But sometimes i need to make an algorithm like this: "find the divs with id point and must begin with REAL POINT: "

We can find

$point = $html->find('div[id=point]')->innertext=' REAL POINT:';

But that finds only divs include ' REAL POINT:'

But i have to find divs innertext begin 'REAL POINT:'

How can i find?

2
  • 3
    You shouldn't have multiple elements with the same id, they are supposed to be unique. I suggest you use <div class="point"> instead. Even better <div class="point"> and <div class="realpoint"> . Commented Aug 16, 2011 at 15:21
  • I don't already. Target source does it :) Commented Aug 16, 2011 at 15:32

3 Answers 3

1

You could use stripos for case sensitive.

foreach($html->find('div[id=point]') as $element) {
    if ( strpos($element->innertext, 'REAL POINT:') !== FALSE ) {
        // something here
    }
}

You could also do a search for the string exactly at the start:

foreach($html->find('div[id=point]') as $element) {
    if ( strpos($element->innertext, 'REAL POINT:') === 0 ) {
        // something here
    }
}

But if you want to remove whitespace before the first character in a div:

foreach($html->find('div[id=point]') as $element) {
    if ( strpos(trim($element->innertext), 'REAL POINT:') === 0 ) {
        // something here
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is semi answer Aram. I wish there was ->innertext^='REAL POINT'; But it doesn't work.
Take a look at simplehtmldom.sourceforge.net and the examples that come with the download. They seem to foreach loops a fair bit, but yeah, they should have provided something like that.
0

Use DOMDocument and DOMXPath:

Example (http://codepad.org/pkdd3Suz):

<?php

$html = <<<END
<html>
    <head>
        <title>Sample</title>
    </head>
    <body>
        <div id="point">9</div>
        <div id="point">REAL POINT: 9</div>
    </body>
</html>
END;

$doc = new DOMDocument;
$doc->loadHTML($html);

$xpath = new DOMXPath($doc);
$nodes = $xpath->query('//div[@id="point" and starts-with(., "REAL POINT:")]');

if ( $nodes )
    foreach ( $nodes as $node )
        echo $node->textContent . PHP_EOL;

2 Comments

This code works fine but not with this HTML sample, which is invalid and triggers a warning: ID point already defined
probably a better class than simple html dom parser
0

Using XPath:

//div[@id='point' and starts-with(., 'REAL POINT:')]

2 Comments

This answer is incorrect. The starts-with function requires at least 2 arguments. Source: http://www.w3.org/TR/xpath-functions/#func-starts-with.
Yes and it is not simple html dom parser's element.

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.