有一个客户来找我关于那里的网站,他们正在使用 Pulse CMS,他们想在主页上显示最近的帖子,很容易......但我看不出如何显示一小部分帖子。我的php知识有限。
由此:
帖子标题 01/02/03
看到所有
对此:
帖子标题 01/02/03
新的黎明新的一天,最近的帖子内容...
看到所有
这是代码:
<?php
error_reporting(0);
include_once("config.php");
include_once("path.php");
include_once("helpers/blog_lib.php");
include_once("helpers/functions.php");
if($pulse_lang == 0){
include_once("lang.php");}
else
if($pulse_lang == 1){
include_once("lang_de.php");}
$blog_file = new read_File("blog");$amount = $blog_file->countLines();
$lines = $blog_file->getLines();$amount_view = 1 ;
echo "<div class=\"span3 latest-post-1\">\n\n";
for ($n=$amount-1 ; $n>=($amount - $amount_view) ; $n-- ) {
$blog = explode("|", $lines[$n]);
if (isset($blog[0]) & $blog[0] != '') {
$date = explode( ' ' , $blog[2]);
$date = $date[2] . ' ' . $date[1] . ' ' . $date[3];
include("clean_url.php");
if($rewrite) {
if ($blog_comments) {
echo "<p><a href=\"/blog-". $blog[0] ."-".cleanUrlname($blog[3])."\">" .$blog[3]. "</a> (" .$blog[1]. ")<br>";
} else {
echo "<p><a href=\"/blog-". $blog[0] ."-".cleanUrlname($blog[3])."\">" .$blog[3]. "</a><br>";
}
} else {
if ($blog_comments) {
echo "<p><a href=\"". $blog_url ."?d=". $blog[0] ."\">" .$blog[3]."</a> (" .$blog[1]. ")<br>";
} else {
echo "<p><a href=\"". $blog_url ."?d=". $blog[0] ."\">" .$blog[3]."</a><br>";
}
}
if($date_format) {
echo date("d.m.Y", strtotime($date))."</p>\n\n";
} else {
echo date("m-d-Y", strtotime($date))."</p>\n\n";
}
}
}
echo "<p class=\"all-posts\"><a href=\"". $blog_url ."\">$lang_blog_see_all</a></p>";
echo "\n\n</div>";
?>
它只是从 .txt 文件中获取博客文章的标题和日期,因此肯定有人可以将文章的“100”个字符拉出来。
编辑
这是读取文件的代码:
<div id="fb-root"></div>
<script>
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>
<?php
error_reporting(0);
include_once("config.php");
include_once("path.php");
if($pulse_lang == 0){ include_once("lang.php");} elseif($pulse_lang == 1){ include_once("lang_de.php");}
include_once("helpers/blog_lib.php");
include_once("helpers/functions.php");
if(!isset($_POST["question"])){ $question = array_rand($questions, 1); $answer = strtolower($questions[$question]); }
?>
<link rel="alternate" title="RSS" href="http://<?php echo $_SERVER['HTTP_HOST']; ?>/<?php echo $pulse_dir; ?>/rss.php" type="application/rss+xml" >
<link href="http://<?php echo $_SERVER['HTTP_HOST']; ?>/<?php echo $pulse_dir; ?>/css/blog.css" rel="stylesheet" >
<div class="blog-wrap">
<?php
$blog_file_1 = new read_File("blog");
$amount = $blog_file_1->countLines();
$lines = $blog_file_1->getLines();
//id given - validating new comment and displaying a sepcific blog post
if(isset($_GET["d"]) && strlen($_GET["d"] > 0) && is_numeric($_GET["d"]) ) { $id_post = super_clean($_GET["d"]);
// security
if(!empty($_POST["answers"])
&& $questions[stripslashes(html_entity_decode($_POST["question"]))] == strtolower(trim($_POST["answers"]))
&& md5($questions[stripslashes(html_entity_decode($_POST["question"]))]) == $_POST["token"]) {
$resp = 1;
$question = array_rand($questions, 1);
$answer = strtolower($questions[$question]);
} elseif (isset($_POST["answers"])) {
$resp = 2;
$question = array_rand($questions, 1);
$answer = strtolower($questions[$question]);
}
if($blog_capcha == false){ $resp = 1;}
if (strlen($_POST['comment'])> 1000){ $too_long = true; }
//1.validate and save new comment -id and form
if(!empty($_POST['name']) && !empty($_POST['mail']) && !empty($_POST['comment']) && empty($_POST['ph']) && $blog_comments && ($too_long != true) ) {
//sanatizing content
$email = strip_tags(htmlspecialchars($_POST['mail'], ENT_QUOTES, 'UTF-8'));
$name = strip_tags(trim(stripslashes($_POST['name'])));
$comment = strip_tags(stripslashes($_POST['comment']),'<p><br><a>');
$comment = htmlspecialchars($comment, ENT_QUOTES, 'UTF-8');
$x=0;// for mail
if(preg_match("/@/", $_POST['mail'])) {
if($resp == 1) {
$x=1;
$comment = str_replace("\n", "<br />", $comment);
$comment = str_replace("\r", "", $comment);
$comment = str_replace("|", "¦", $comment);
$postdate = date('D, d M Y H:i:s O');
$blog = $id_post."|".$postdate."|".$name."|".$email."|".$comment."\n";
//check if comment already exists
$comment_saved = 0;
$comment_file_1 = new read_File("comment");
$amount_c = $comment_file_1->countLines();
$lines_c = $comment_file_1->getLines();
for($i=0; $i < $amount_c; $i++) {
$comments = explode("|", $lines_c[$i]);
if($comments[0] == $id_post) {
if(($comments[0] == $id_post) && ($comments[2] == $name) && ($comments[3] == $email) && ($comments[4] == $comment)){
$comment_saved = 1;
}
}
}
//validated new comment; save it
if ($comment_saved == 0){
$add = new add_Content("comment", $blog);
echo $add->appendData();
//update Number of comments
for($i=0; $i < $amount; $i++) {
$blogg = explode("|", $lines[$i]);
if($blogg[0] == $id_post){
$blogg[1]++;
$new_data .= $blogg[0]. "|" . $blogg[1] ."|".$blogg[2]."|".$blogg[3]."|".$blogg[4]."|".$blogg[5]."\n";
} elseif($blogg[0] != "") {
$new_data .= $blogg[0]. "|" . $blogg[1] ."|".$blogg[2]."|".$blogg[3]."|".$blogg[4]."|".$blogg[5]."\n";
}
}
$write = new add_Content("blog", $new_data);
echo $write->writeData();
//send mail
if($x===1){
for($i=0; $i < $amount; $i++) {
$b = explode("|", $lines[$i]);
if($b[0] == $id_post){
$title = $b[3];
}
}
$comment_dec = trim(strip_tags(stripslashes($_POST['comment'])));
$sender_Name = "PulseCMS";
$sender_email = $email_contact;
$recipient = $email_contact;
$mail_body = $lang_blog_notify."\n\n";
$mail_body .= $lang_blog_title.": ".$title."\n\n";
$mail_body .= $lang_blog_name.": ".$name."\n\n";
$mail_body .= $lang_blog_comment.": ".$comment_dec."\n\n";
$subject = $lang_blog_subject;
$header = "From: ". $sender_Name . " <" . $sender_email . ">\r\n";
mail($recipient, $subject, $mail_body, $header);
}//end mail
} //end save new comment
unset($email);
unset($name);
unset($comment);
unset($_POST['submit']);
unset($comment_file_1);
$_POST['name'] = '';
$_POST['mail'] = '';
$_POST['comment'] = '';
} // end resp
} else { $error = 1;}//end pregmatch email
} else //end validating and saving new comment
//2.show specific blog post and comments - id
$no_posts_found=0;
$blog_file_1 = new read_File("blog");
$amount = $blog_file_1->countLines();
$lines = $blog_file_1->getLines();
for($i=0; $i < $amount; $i++) {
$blog = explode("|", $lines[$i]);
if($blog[0] == $id_post) {
$no_posts_found++;
$date = explode( ' ' , $blog[2]);
$date = $date[2] . ' ' . $date[1] . ' ' . $date[3];
$title = cleanUrlname($blog[3]);
echo "<h2 class=\"blog-title\">" .$blog[3]."</h2>\n";
if($date_format) { echo "<div class=\"blog-date\">" . date("d-m-Y", strtotime($date))."</div>\n"; }
else { echo "<div class=\"blog-date\">" . date("m-d-Y", strtotime($date))."</div>\n"; }
echo "<div class=\"blog-content\">" . str_replace("##more##", "", $blog[4]) ."</div>\n\n";
break;
}
} if($no_posts_found == 0){
echo "404 ~does not~ exist.";
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
die();
}
echo "<script>document.title = \"$blog[3]\";</script>\n";
?>
<div class="share-buttons ">
<div style='float:left;pa dding-right:15px'>
<div class="g-plusone" da ta-size="medium" data-annotation="none"></div></div >
<div style='float:left;pa dding-right:15px'>
<a a l ign="bottom" href="h ttps://twitter.com/share" class="twitter-share-butt o n" data-count="none" >Tweet</a>
<scr i pt src="//platform.t witter.com/widgets.js"></script></div>
<div style='float:left;'>
<fb: l ike colorscheme='light' expr:hr ef='data:post.url' font='' data- widt h ="300" layout='button_count' se nd='false' show_faces='false'/></div>< /div >
<?ph p
// show comments
if($blog_comments) {
echo "<div class=\"comments\" id=\"c omments\">";
$comment _file_2 = new re ad_File( "comment");
$amount_ c_2 = $comment_f ile_2->c ountLines();
$lines_c _2 = $comment_fi le_2->ge tLines();
for($i=0 ; $i < $amount_c_2; $i++ ) {
$comments = explode("|", $lines_c_2[$i]);
if($ comments[0] == $id_post) {
$date = explode( ' ' , $ comments[1]);
$date = $date[2] . ' ' . $date[1] . ' ' . $date[3];
echo "<div class=\"comme nt-line\">\n";
if($date_format) { echo "<div class=\"comment-date\">". date("d-m-Y", strtotime( $date)) ."</div>\n"; }
else { echo " <div cl ass=\"comment-date\">". date("m-d-Y", strtotime($da te))."< /div>\n"; }
echo "<di v class =\"comment-name\">". $comments[2]."</div>\n";
echo "</div>\n";
echo "<div class=\"blog-comment\">" .$comment s[4]."< /div>\n\n";
}
}
//display comment form only if s et to enable
for($i=0; $i < $amount; $i++) {
$blog = explode("|", $lines[$i]);
if( ($blog[0] == $id_post) && ($blog[5] == 1) ){ echo "<p class=\"comments-off\"> $lang_blog_off_comments</p>"; }
if( ($blog[0] == $id_post) && ($blog[5] != 1) ){
?>
<form class="comment-form" action="<?php if($rewrite) {e cho "blog-$id_post-".cleanU rlname( $blog[3]);} else {echo "?d=$id_post";}?>" method="post">
<?php $_POST = array_map('trim', $_POST);?>
<p class="add"><b><?php echo $lang_blog_add_comment;?></b></p>
<p><label class="comment-label"><?php echo $lang_blog_label_name;
if(isset($_POST['submit']) && empty($_POST['name'])) { echo "<span style=\"color:red\"> - $lang_blog_error_name</span>";}?></label><br />
<input type="text" name="name" size="45" value="<?php echo strip_tags(trim(strips lashes ($_POST['name']))); ?>" /></p>
<p><label cla ss="comment-label"><?php echo $lang_blog_label_email;
i f((isset($_POST['submit']) && empty($_POST['mail']))|| $error==1) {e cho "< s pan style=\"color:red\"> - $lang_blog_error_email</span>";}?></label ><br / >
<input size="45" type="text" name="mail" value="<?php echo strip_tags(trim(strips lashes ($_POST['mail']))); ?>" /></p>
<p><label class="comment-label"><?php echo $la ng_blog_label_comment;
if(isset($_POST['submit']) && empt y($_POST['comment'])){ ec ho "<span style=\"color:red\"> - $lang_blog _error_comment</span>";}
if ($too_long == true){echo "<span style=\"color:red\"> -Your comment is too long</span>";}?></label><br />
<textarea name="comment" cols="60" rows="7"><?php echo strip_tags(trim(stripslashes ( $_POST['comment']))); ?></textarea></p>
<p><input id="ph" size="45" type="text" name="ph" value="<?php echo strip_tags(trim ( stripslashes($_POST['ph']))); ?>" /></p>
<?php if( ($blog_capcha == true) || (!isset($blo g_capcha)) ){ ?>
<p><label class="comment-label"><?php ec ho $question; if ($resp == 2) {e cho "<span style=\"color:red\"> - $lang_blog_error _captcha</span>";}?> </label><br />
<input type="hidden" name="token" value= "<?php echo md5($answer); ?>" />
<input type="hidden" name="question" val ue="<?php echo htmlentities($question); ?>" / >
<input type="text" name="answers" /></p>
<?php } ?>
<p><button class="btn" type="submit" name ="submit"><?php echo $lang_blog_submit; ?>< /button></p>
</form>
<?php
}//blogcomm ents enables
}// end comment and ip-post
echo '</div>';
}}//end display commment form if enabled
//s earch
els e if(isset($_GET['search']) && strlen($_GET['search'])>0 && strlen($_GET['search'])<50 ) {
include("blog_search.php");
}
//blog default
else {
?><a class="search-icon" href="#search">
<img src="http://<?php echo $_SERVER['HTTP_HOST']; ?>/
<?php echo $pulse_dir;?><?php
$result_per_page = $per_page ;
$total_pages = ceil($amount/$result_per_page);
$cur_page = $_GET[page] ? $_GET[page] : 1;
$start = $amount - (($cur_page-1) * $result_per_page);
$end = $amount - (($cur_page) * $result_per_page);
for ($n=$start-1 ; $n>=$end ; $n-- ) {
$blog = explode("|", $lines[$n]);
if ( isset($blog[0]) & $blog[0] != '') {
$date = explode( ' ' , $blog[2]);
$date = $date[2] . ' ' . $date[1] . ' ' . $date[3];
echo "<div class=\"entry\">\n";
if($rewrite) { echo "<h2 class=\"blog-title\"><a href=\ "blog-". $blog[0] ."-". cleanUrlname($blog[3])."\">" .$blog[3]."</a></h2>\n"; }
else { echo "<h2 class=\"blog-title\"><a href=\"?d=". $blog[0] ."\">" .$blog[ 3]."</a></h2>\n"; }
if($date_format) { echo "<div class=\"blog-date\">" . date("d-m-Y", strtotime($date ))."</div>\n";}
else { echo "<div class=\"blog-date\">" . date("m-d-Y", strtotime($d ate))."< /div>\n"; }
if(preg_match("/^(.*)##more##/U", $blog [4], $m)) {
if($rewrite) { echo "<div class =\"blog-content\">" . $m[1] ." <a href=\"blog-". $blog[0] ."-".cl eanUrlname($blog[3])."\" class=\"read-more\" >$lang_blog_more</a></div>\n";}
else { echo "<div class=\"blog-content\">" . $m[1] ." <a href=\"?d=". $blog[0] ."\" class=\"read-more\">$lang_blog_more</a></div>\n";}
} else { echo "<div class=\"blog-content\">" . $blog[4] ."</div>\n"; }
if($blog_commen ts) {
if($blog[1] ) {
if($rew rite) { echo "<div class=\"num_ comments\"><a href=\" blog-". $blog[0 ] ."-". cleanUrlname($blog[3])."#comment s\">(". $blog[1] .") $lang_b log_num_comment</a></div>\n\n";}
else {echo "<div class=\"num_comments\"><a href=\"?d=". $blog[0] . "#com ments\">(". $blog[1] .") $lang_blog_num_comment</a></div>\n"; }
} else {
if($rewrite) { ec ho "<div class=\"num_comments\"><a href=\"blog-". $blog[0] ."-" .cle anUrlname($blog[3])."#comments\">$lang_blog_no_comment </a></div>\n\ n";}
else { e cho "<div class=\"num_comments\"><a href=\"?d=". $blog[0] ."#comme nts\">$lang_blog_no_comment</a></div>"; }
}
}
ec ho "</div> \n\n";
}// en d if blog0
else { continue; }
}//end for
echo '<div class="blog-pagination ">';
if ($cur_page<$total_pages) {
if($rewrite) { echo "<a href=\"". $prefix ."blog-p age-" . ($cur_page+1) . "\">" . $lang_blog_older . "</a>" . " ";}
else { echo "<a href=\"?page=" . ($cur_page+1) . "\">" . $lang_blog_older . "< /a>" . " ";}
}
if ($cur_page>1) {
if($rewrite) { echo "<a href =\"". $prefix ."blog-page-" . ($cur_page-1) . "\">" . $lang_blog_newer . "</a>"; }
else { echo "<a href=\"?page=" . ($cur_page-1) . "\">" . $lang_blog_newer . "< /a>" . " "; }
}
echo '</div>';
?>
<form id="searc h" name="blog_search" action="<?php echo htmlentities($_SERVER['PHP_SEL F']); ?> " method="get">
<input id="searchword" name="search" type="text" maxlength="50" value="<?php echo super_clean($_GET['search']); ?>" placeholder="<?php echo $lang_blog_search; ?>">
</form>
<?php
}//default: blog post collection
echo '</div>';
?>
*重新编辑*
<?php
class read_File{
function __construct($name){
$this->name = $name;
if($this->name == "blog"){$this->name = ROOTPATH . "/data/blog/blogfile.txt";}
else if($this->name == "comment"){$this->name = ROOTPATH . "/data/blog/comments.txt";}
else { echo $lang_blog_error_reading;}
}
function getData(){
$open = fopen($this->name , "r");
$data = @fread($open, filesize($this->name));
fclose($open);
return $data;
}
function getLines(){
$lines = explode("\n", $this->getData());
return $lines;
}
function countLines(){
$amount_of_lines = count($this->getLines());
return $amount_of_lines-1;
}
}//end class
class add_Content{
function __construct($name,$data){
$this->name = $name;
$this->data = $data;
if($this->name == "blog"){$this->name = ROOTPATH . "/data/blog/blogfile.txt";}
else if($this->name == "comment"){$this->name = ROOTPATH . "/data/blog/comments.txt";}
else { echo $lang_blog_error_reading;}
}
function appendData(){
$open = fopen($this->name, "a");
fwrite($open, $this->data);
fclose($open);
}
function writeData(){
$open = fopen($this->name, "w");
fwrite($open, $this->data);
fclose($open);
}
}//end class
//sanatize input
function super_clean($text){
$text = trim(stripslashes(strip_tags(htmlspecialchars($text, ENT_QUOTES, 'UTF-8'))));
return $text;
}
?>
- 编辑 *
var_dump 输出:
array(6) { [0]=> string(1) "5" [1]=> string(1) "0" [2]=> string(31) "Fri, 11 Oct 2013 13:04:17 +0200" [3]=> string(7) "Subject" [4]=> string(54) "
This is a sample blog post for Pulse CMS Pro.
" [5]=> string(1) "0" }
博客文件行:
4|0|Fri, 11 Oct 2013 12:57:09 +0200|hey this works|<p> Lorem ipsum dolor sit</p>|0
5|0|Fri, 11 Oct 2013 13:04:17 +0200|Subject|<p> This is a sample blog post for Pulse CMS Pro.</p>|0