Skip to content

Instantly share code, notes, and snippets.

@axelrindle
Last active May 27, 2018
Embed
What would you like to do?
Small PHP Blog System
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ viewpost.php?p=$2 [L,QSA]
foreach($posts as $post) {
echo '<div class="post">';
echo $Parsedown->text(limit_text(file_get_contents("posts/" . $post->file), 80)); //replace '80' with your own number
echo '<hr>';
echo '<div class="btns">';
echo '<div class="space"></div>';
echo '<a class="show" href="/blog-system/' . str_replace(".md", "", $post->file) . '">View post</a>';
echo '</div>';
echo '</div>';
}
<?php
//#######################
//# BLOG SYSTEM #
//#######################
/**
* A placeholder class for a post.
*/
class Post {
/** The file for this post */
public $file;
/** Constructor */
function __construct($file) {
$this->file = $file;
}
}
/**
* Returns all files in a directory.
*
* @param string [$dir = '.'] The directory to look for files in.
* @return array An array with all file names in the specified directory.
*/
function fileList($dir = '.') {
$files = array();
if ($handle = opendir($dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." and $entry != "..") {
array_push($files, $entry);
}
}
closedir($handle);
}
return $files;
}
/**
* Truncates the text at a specified word count.
*
* @param string $text The text to truncate.
* @param int $limit The maximum count of words.
* @return string The truncated text.
*/
function limit_text($text, $limit) {
if (str_word_count($text, 0) > $limit) {
$words = str_word_count($text, 2);
$pos = array_keys($words);
$text = substr($text, 0, $pos[$limit]) . '...';
}
return $text;
}
/** An array to store the posts in */
$posts = array();
/**
* Adds a post to our global list.
*
* @param string $file The post file.
*/
function addPost($file) {
global $posts;
$newpost = new Post($file);
array_push($posts, $newpost);
}
@import url(https://fonts.googleapis.com/css?family=Source+Code+Pro);
.container {
background-color: #e2e1e0;
}
.posts {
display: flex;
flex-direction: column-reverse;
align-items: center;
}
.post {
width: 800px;
border-radius: 2px;
box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
padding: 20px;
padding-bottom: 30px;
background-color: #fff;
text-align: left;
display: flex;
flex-direction: column;
margin-bottom: 30px;
}
.post h1 {
font-size: 27px;
font-weight: 500;
align-items: center;
}
.post hr {
border-top: 2px solid #263238;
margin-bottom: 30px;
margin-top: 30px;
margin-left: 0;
margin-right: 0;
}
.post .btns {
display: flex;
flex-direction: row;
}
.post .btns .space {
flex: 1 0 auto;
}
.post .btns .show {
border-radius: 2px;
box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
background-color: #e91e63;
padding: 12px;
cursor: pointer;
color: #fff;
}
.post .btns .show:hover {
color: #fff;
background-color: #ce1957;
}
.post pre code {
overflow-x: auto;
overflow-y: hidden;
padding: 15px;
font-size: 15px;
font-family: 'Source Code Pro';
display: block;
background-color: #e8e8e8;
}
@media screen and (max-width: 850px) {
.post {
width: auto;
}
}
.container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
.post {
width: 1000px;
}
@media screen and (max-width: 1100px) {
.post {
width: 90%;
}
}
<?php
echo '<div class="post">';
$matches = array();
preg_match("/[^\/]+$/", $_SERVER['REQUEST_URI'], $matches); // assuming that the url's last parameter is the post name WITHOUT the .md extension
$file = $matches[0];
echo $Parsedown->text(file_get_contents('posts/' . $file . '.md'));
echo '</div>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment