0

Possible Duplicate:
How to parse and process HTML with PHP?

I am selecting a column from a mySQL table which has a value something like this:

<div class="article news group">
    <p class="news_info">18/10/12</p>
    <h2>Hello world!</h2>
    <p>ertwrt aerteartert</p>
    <p>ertaertert</p>
    <p>waertwertwertwaertweart</p>
</div>

I now need to only get the 3 p tags from this - so returning:

    <p>ertwrt aerteartert</p>
    <p>ertaertert</p>
    <p>waertwertwertwaertweart</p>

Is there an easy way to do this with php or will I need to use jQuery to remove the unwanted code.

Either way, any ideas how I would do it?

Community
  • 1
  • 1
Tom
  • 12,776
  • 48
  • 145
  • 240
  • 1
    It's better to do it with PHP if it's unwanted. No reason to send it to the client (more traffic) and then remove it. PHP & regular expressions are what you need in this instance. – Reinstate Monica Cellio Oct 18 '12 at 10:42

3 Answers3

2

Using PHP

$html = '<div class="article news group">
    <p class="news_info">18/10/12</p>
    <h2>Hello world!</h2>
    <p>ertwrt aerteartert</p>
    <p>ertaertert</p>
    <p>waertwertwertwaertweart</p>
</div>';

preg_match_all('~(<p>.+?</p>)~', $html, $match);

echo implode("\n", $match[1]);
PyQL
  • 1,830
  • 3
  • 18
  • 22
0

This may be help for your needs : http://api.jquery.com/siblings/

senthilbp
  • 807
  • 1
  • 9
  • 16
0

If the html you are processing is more complicated than the example you might want to consider QueryPath (http://querypath.org/)

It would be overkill for your example but can be quite useful for this type of manipulation.

Rick Burgess
  • 704
  • 1
  • 5
  • 12