0

I've been hitting a brick wall with trying to use the following regex in PHP.

<li.*?class="(.*?gchoice.*?)">((.|\n)*?)<\/li>

This works fine in the following test selecting only the nested list item when i try it in a jquery based tester

 <li class="gfield gfield_contains_required" id="field_1_17">
<label class="gfield_label">Categories<span class="gfield_required"> (Required) </span>        </label>
<div class="ginput_container">
<ul id="input_1_17" class="gfield_checkbox">
<li class="gchoice_17_1">
    <input type="checkbox" id="choice_17_1" value="First Choice" name="input_17.1">
    <label id="label_17_1" for="choice_17_1">First Choice</label>
</li>
</ul>
</div>
</li>

However in PHP it just doesn't want to work no matter how I try to escape the characters and no matter the delimiter I try and use. From what I know I should only need a / or # at the stand and end, however when that did not work I tried escaping the brackets as well and yet it still didn't work.

1
  • 2
    Are you sure you want to use regex to parse HTML? Commented Jun 11, 2014 at 10:45

1 Answer 1

6

Don't use regex.

$dom = new DOMDocument();
$dom->loadHTML($your_html_source);
$xpath = new DOMXPath($dom);
$target = $xpath->query("//li[contains(@class,'gchoice')]");
foreach($target as $node) {
    var_dump($node); // do something
}
Sign up to request clarification or add additional context in comments.

5 Comments

So much better. :) @ChrisMorris I removed my answer, but for your reference look up the (?s) modifier.
I can't do this as I need the regex to pick up the rest of the class name, the name is dynamic so I only know the first part, Unless there is a way with the DOM method to wildcard the class name.
@ChrisMorris The answer uses the Contains function. So it looks for li nodes that has a class attribute that includes the substring gchoice. What further need for wildcards do you need? Once you have the node extracting the complete class attribute should be straight forward.
Sorry I've not used DOM I'm struggling to see how I could use this as I'm also trying to replace that node with a preg_replace later in my code.
@ChrisMorris Keep using DOMDocument functions, as they will ensure that your result is valid no matter what.

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.