-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredditParse.php
executable file
·40 lines (35 loc) · 1.09 KB
/
redditParse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL & ~E_NOTICE);
class Post {
var $author;
var $category;
var $content;
var $updated;
var $title;
}
function escapeAlexa($str) {
//escape weird quotes and replace " with ' because json
$str = preg_replace("~‘|’|“|”|\"~", "'", $str);
//get rid of any other weird characters
$str = preg_replace("/[^a-zA-Z0-9 \/,.:']/", "", $str);
return $str;
}
function getPosts($subreddit = NULL) {
$sub = "";
if ($subreddit !== NULL)
$sub = "r/$subreddit/";
$x = simplexml_load_file("https://www.reddit.com/$sub.rss");
$posts = array();
foreach ($x->entry as $entry) {
$post = new Post();
$post->author = escapeAlexa((string) $entry->author->name);
$post->category = escapeAlexa((string) $entry->category["label"]);
$post->content = escapeAlexa((string) $entry->content);
$post->updated = escapeAlexa((string) $entry->updated);
$post->title = escapeAlexa((string) $entry->title);
array_push($posts, $post);
};
return $posts;
}
?>