3

I'm hoping someone can help me. I'm using PHP Simple HTML DOM Parser (http://simplehtmldom.sourceforge.net/manual.htm) successfully, but I now am trying to find elements based on a certain name. For example, in the fetched HTML, there might be a tags such as:

<p class="mattFacer">Matt Facer</p>
<p class="mattJones">Matt Jones</p>
<p class="daveSmith">DaveS Smith</p>

What I need to do is to read in this HTML and capture any HTML elements which match anything beginning with the word, "matt"

I've tried

$html = str_get_html("http://www.testsite.com");
foreach($html->find('matt*') as $element) {
   echo $element;
}

but this doesn't work. It returns nothing.

Is it possible to do this? I basically want to search for any HTML element which contains the word "matt". It could be a span, div or p.

I'm at a dead end here!

1
  • With XPath (which I'm not sure if the Simple DOM supports), you'd just do //p[starts-with(@class,'matt')] Commented May 11, 2011 at 19:34

2 Answers 2

3
$html = str_get_html("http://www.testsite.com");
foreach($html->find('[class*=matt]') as $element) {
   echo $element;
}

Let's try that

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

1 Comment

thanks - yes that's worked! Is it possible to do this if I don't know if it's the class name or the id? eg: <p id="mattFacer"> or <p class="mattFacer">
0

Maybe this?

foreach(array_merge($html->find('[class*=matt]'),$html->find('[id*=matt]')) as $element) {
    echo $element;
}

Comments

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.