1

I'm trying to parse some information from an HTML page. The only problem is that the information I need is not in a tag so it can't be easily found. Here is an example of what I am talking about.

<span class="fieldlabeltext">Levels: </span>Undergraduate 
<br>
<span class="fieldlabeltext">Attributes: </span>Online Course 
<br>
<span class="fieldlabeltext">Instructors: </span>N/A
<br>

I need to extract "Online Course" from the example above, but not all of the "Attributes" are the same throughout the entire HTML file. So some could be maybe "Critical Thinking" or "Capstone", and many more other titles. What would be the best way to go about extracting this data? I am using the PHP Simple HTML DOM Parser - http://simplehtmldom.sourceforge.net/

1
  • 2
    Once you find a node in a dom tree, there's nextsibling and previoussibling to look at that node's neighbors in the tree. Don't know if simpledom has that, but the full DOM does. Commented Mar 16, 2012 at 18:30

1 Answer 1

1

Marc B's comment is right-on. SimpleHTMLDOM has the following functions that you can perform on elements to accomplish what you want.

  • element $e->parent() - Returns the parent of element.
  • element $e->first_child() - Returns the first child of element, or null if not found.
  • element $e->last_child() - Returns the last child of element, or null if not found.
  • element $e->next_sibling() - Returns the next sibling of element, or null if not found.
  • element $e->prev_sibling() - Returns the previous sibling of element, or null if not found.

Source: http://simplehtmldom.sourceforge.net/manual.htm#section_traverse

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

1 Comment

@Fitz how can i get the text if the next element is not as like previous eg. if my current one is <strong>a:</strong>test <li>test</li> ?

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.