Advertisement
dimipan80

Facebook Posts

May 1st, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1. <?php
  2. date_default_timezone_set('Europe/Sofia');
  3. $text = $_GET['text'];
  4. $postPattern = "/\s*(.+?)\s*;\s*(\d{1,2}\-\d{1,2}\-\d{4})\s*;\s*(.+?)\s*;\s*(\d{1,4})\s*;\s*(.*)\s*/";
  5.  
  6. preg_match_all($postPattern, $text, $matches, PREG_SET_ORDER);
  7. $resultPosts = array();
  8. foreach ($matches as $match) {
  9.     $author = htmlspecialchars($match[1]);
  10.     $post = htmlspecialchars($match[3]);
  11.     $likesCount = (int)$match[4];
  12.     $comments = preg_split('/\s*\/\s*/', $match[5], -1, PREG_SPLIT_NO_EMPTY);
  13.     $time = strtotime($match[2]);
  14.     $date = date("j F Y", $time);
  15.     $resultPosts[$time] = array($author, $date, $post, $likesCount, $comments);
  16. }
  17.  
  18. krsort($resultPosts);
  19. foreach ($resultPosts as $postInfo) {
  20.     echo "<article><header><span>{$postInfo[0]}</span><time>{$postInfo[1]}</time></header>";
  21.     echo "<main><p>{$postInfo[2]}</p></main><footer><div class=\"likes\">{$postInfo[3]} people like this</div>";
  22.     if (count($postInfo[4]) > 0) {
  23.         echo '<div class="comments">';
  24.         foreach ($postInfo[4] as $comment) {
  25.             echo '<p>' . htmlspecialchars(trim($comment)) . '</p>';
  26.         }
  27.         echo '</div>';
  28.     }
  29.  
  30.     echo '</footer></article>';
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement