0

I have a text like below

$str = '<div>
           <div id="priceRangeWrapper">
              <div id="priceSlider" min="0" max="0"></div>
           </div>
        </div>';

1) First I want to get the position of <div id="priceSlider" min="0" max="0"></div> from above string where min and max values are random. Something like with strpos() function of Php in which it returns the position in terms of int like

 $pos = strpos($str, '<div id="priceSlider" min="0" max="0"></div>');
 //but min and max values are random. I don't know what can be they

2) I want to get min and max values from above text. How can I get these two values with/without regex in PHP?

4
  • 4
    Did you try DOMDocument? Commented Dec 18, 2015 at 18:00
  • I once heard it is over killing for such task as compared to regex. But I havn't tried yet. Let me check. And is it right that's over killing than regex searching? Commented Dec 18, 2015 at 18:02
  • 1
    @AbdulJabbarWebBestow You will get varying opinions on whether or not its overkill, so take opinions with a grain of salt. The benefit to properly parsing HTML is that you have a well-defined interface to access the data, which has better error correction and handling of the input (since it has a better understanding of HTML than any regex you craft will). These characteristics make it more maintainable and explicit as to your intentions, which is why I will always prefer to use DOMDocument in these situations than a regex. Commented Dec 18, 2015 at 18:09
  • @nickb just out of curiosity, wondering if DOMDocument is using regex on backend to find Paths and Attributes etc? I mean to say that how it would be searching for elements and their attribute etc without using regex? Commented Dec 18, 2015 at 18:10

1 Answer 1

5

Don't use a regex to parse HTML. Instead, here's an example with DOMDocument.

$doc = new DOMDocument();
$doc->loadHTML($str); // Load the HTML string from your post

$xpath = new DOMXPath($doc);
$node = $xpath->query('//div[@id="priceSlider"]')->item(0); // Get the <div>

// Print out the min and max attribute values
echo $node->getAttribute('min') . " " . $node->getAttribute('max');

You can see it working here.

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

5 Comments

just out of curiosity, wondering if it is using regex on backend to find Paths and Attributes etc? I mean to say that how it would be searching for elements and their attribute etc without using regex?
@AbdulJabbarWebBestow No, it's not. It parses the HTML into a DOM-like structure, which we can then operate on.
@AbdulJabbarWebBestow likely, no. It's more likely using a lexer/parser that was designed especially for handling html. It's a lot easier to parse HTML this way. If i'm wrong, someone please correct me.
I am probably learning new thing DOMDocument today. Thanks to you and @stribizhev. I had been using Regex to manipulate my HTML between documents. Let's see if someone else has better solution than DOMDocument.
@AbdulJabbarWebBestow DOMDocument is probably the best solution for handling HTML pages, actually. It's pretty much the standard way to do it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.