M
M
Morgan_sh2014-05-11 01:45:07
PHP
Morgan_sh, 2014-05-11 01:45:07

How to parse a forum post using PHP Simple HTML DOM Parser?

I need help, I need to parse the text of messages from a forum thread of only 1 author.
For example, from the forum
only the author "sakakolm"

Answer the question

In order to leave comments, you need to log in

4 answer(s)
T
Twist, 2014-05-11
@Morgan_sh

I would advise you to look in the direction of phpQuery, especially if you have ever worked with jQuery
Only I was too lazy to play with the encoding, but I think this will no longer be a problem :)

<?
require_once 'phpQuery.php';

$url = 'http://ruforum.mt5.com/threads/2494-obzor-valyutnogo-rinka-za-nedelyu';

$userName = 'almostsuper';


$htmlPage = file_get_contents($url);
$html = phpQuery::newDocument($htmlPage);

$posts = $html->find('.postdetails');


foreach ($posts as $var) {

  $userPost = pq($var)->find('.username > strong:contains('.$userName.')');
  if($userPost->text()==$userName)
  {
    echo '<h1>'.$userName.'</h1>';
    $postBody = pq($var)->find('.postbody');
    echo $postBody.'<hr/>';
  }

}

A
Alexander Zelenin, 2014-05-11
@zelenin

look for an example of using dom parser.
Or say what is the problem that served to write the question here.

M
Morgan_sh, 2014-05-11
@Morgan_sh

I can't set a regular expression to parse a message with the id= user condition

P
peter23, 2014-05-11
@peter23

One expression is not enough here. Something like this:

$rows = $html->find('ol#posts > li > div.postdetails > div.userinfo > div.username_container > div > a.username[href*=3606-sakakolm]');
foreach($rows as $row) {
  $msg_html = $row->parent()->parent()->parent()->next()->find('div.postrow > div.content');
}

It would be nice to use closest() instead of the parent() heap, but it seems to be absent in the PHP Simple HTML DOM Parser, as well as parents().
The code has not been tested, errors are possible. But I think the idea is clear.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question