Advertisement
Aleksiev

PHP Exam - Facebook Posts

Sep 5th, 2014
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.49 KB | None | 0 0
  1. <?php
  2.  
  3. date_default_timezone_set('Europe/Sofia');
  4.  
  5. $text = trim($_GET['text']);
  6. $postsList = preg_split('/\r?\n/', $text, -1, PREG_SPLIT_NO_EMPTY);
  7. $posts = array();
  8. $result = '';
  9.  
  10. foreach ($postsList as $key => $p) {
  11.    
  12.     $post = explode(';', $p);
  13.     $commentsStr = '';
  14.    
  15.     $posts[$key]['name'] = trim($post[0]);
  16.     $posts[$key]['time'] = strtotime(trim($post[1]));
  17.     $posts[$key]['post'] = trim($post[2]);
  18.     $posts[$key]['likes'] = trim($post[3]);
  19.    
  20.     if ($post[4]) {
  21.         $com = explode('/', $post[4]);
  22.         $comments = '';
  23.  
  24.         foreach ($com as $c) {
  25.             $comments .= '<p>' . htmlspecialchars(trim($c)) . '</p>';
  26.         }
  27.  
  28.         $commentsStr = '<div class="comments">' . $comments . '</div>';
  29.     }
  30.     $posts[$key]['comments'] = $commentsStr;
  31. }
  32.  
  33. $posts = multisort($posts, SORT_DESC, 'time');
  34.  
  35. foreach($posts as $post) {
  36.     $result .= '<article>';
  37.     $result .= '<header><span>' . htmlspecialchars($post['name']) . '</span><time>' . date('j F Y', $post['time']) . '</time></header>';
  38.     $result .= '<main><p>' . htmlspecialchars($post['post']) . '</p></main>';
  39.     $result .= '<footer><div class="likes">' . htmlspecialchars($post['likes']) . ' people like this</div>' . $post['comments'] . '</footer>';
  40.     $result .= '</article>';
  41. }
  42.  
  43. echo $result;
  44.  
  45. function multisort($arr, $ord, $property) {
  46.     foreach ($arr as $id => $val) {
  47.         $key[$id] = $val[$property];
  48.     }
  49.     array_multisort($key, $ord, $arr);
  50.     return $arr;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement