php开发文章发布示例(正则表达式实例开发)
存档:
post.php
<form method="post" action="viewthread.php" target="_blank">
<h2 align="center">发表文章实例开发</h2>
<div style="width:200;float:left">
<h5>选项</h5>
<ul style="list-style:none;margin:0px;padding:0px">
<li><input type="checkbox" name="parse[]" value="1">删除HTML标签</li>
<li><input type="checkbox" name="parse[]" value="2">转换HTML标签为实体</li>
<li><input type="checkbox" name="parse[]" value="3">使用UBB代码</li>
<li><input type="checkbox" name="parse[]" value="4">开启URL识别</li>
<li title="可用的表情:
【:),/wx,微笑】【:@,/fn,发怒】
【:kiss,/kill,/sa,示爱】
【:p,/tx,偷笑】【:q,/dk,大哭】">
<input type="checkbox" name="parse[]" value="5">使用表情</li>
<li><input type="checkbox" name="parse[]" value="6">禁用非法关键字</li>
<li><input type="checkbox" name="parse[]" value="7">PHP代码设为高亮</li>
<li><input type="checkbox" name="parse[]" value="8">原样显示</li>
<li><input type="checkbox" name="parse[]" value="9">同步换行</li>
</ul>
</div>
<div style="width:300;float:left">
<h5>标题<input type="text" name="subject" size=50></h5>
<h5>内容<textarea rows="7" cols="50" name="message"></textarea></h5>
<input type="submit" name="replysubmit" value="发表帖子">
</div>
</form>
viewthread.php
<?php
require "article.class.php";
$article = new Article($_POST["subject"],$_POST["message"],$_POST["parse"]);
echo $article->getSubject();
echo "<hr>";
echo $article->getMessage();
?>
article.class.php
<?php
class Article{
private $subject;
private $message;
function __construct($subject="",$message="",$parse=array()){
$this->subject = $this->html2Text($subject);
if(!empty($parse)){
foreach($parse as $value){
switch($value){
case 1:
$message = $this->delHtmlTags($message);
break;
case 2:
$message = $this->html2Text($message);
break;
case 3:
$message = $this->UBBCode2Html($message);
break;
case 4:
$message = $this->parseURL($message);
break;
case 5:
$message = $this->parseSmilies($message);
break;
case 6:
$message = $this->disableKeyWords($message);
break;
case 7:
$message = $this->prasePHPCode($message);
break;
case 8:
$message = $this->prasePer($message);
break;
case 9:
$message = $this->nltobr($message);
break;
}
}
}
$this->message = $message;
} private function delHtmlTags($message){
return strip_tags($message);
} private function html2Text($message){
return htmlspecialchars(stripslashes($message));
} private function UBBCode2Html($message){
$pattern = array(
'/\[b\]/i','/\[\/b\]/i','/\[i\]/i',
'/\[\/i\]/i','/\[u\]/i','/\[\/u\]/i',
'/\[font=([^\[\<]+?)\]/i',
'/\[color=([#\w]+?)\]/i',
'/\[size=(\d+?)\]/i',
'/\[size=(\d+(\.\d+)?(px|pt|in|cm|mm|pc|em|ex|%)+?)\]/i',
'/\[align=(left|center|right)\]/i',
'/\[url=www.([^\["\']+?)\](.+?)\[\/url\]/is',
'/\[url=(https?|ftp|gopher|news|telnet){1}:\/\/([^\["\']+?)\](.+?)\[\/url]/is',
'/\[email\]\s*([a-z0-9\-_.+]+)@([a-z0-9\-_.]+)\s*\[\/email\]/i',
'/\[email=([a-z0-9\-_.+]+)@([a-z0-9\-_]+[.][a-z0-9\-_.]+)\](.+?)\[\/email\]/is',
'/\[img\](.+?)\[\/img\]/',
'/\[\/color\]/i',
'/\[\/size\]/i',
'/\[\/font\]/i',
'/\[\/align\]/'
); $replace = array(
'<b>','</b>','<i>',
'</i>','<u>','</u>',
'<font face="\\1">',
'<font color="\\1">',
'<font size="\\1">',
'<font style=\"font-size:\\1\">',
'<p align="\\1">',
'<a href="http://www.\\1" target="_blank">\\2</a>',
'<a href="\\1://\\2" target="_blank">\\3</a>',
'<a href="mailto:\\1@\\2">\\1@\\2</a>',
'<a href="mailto:\\1@\\2">\\3</a>',
'<img src="\\1">',
'</font>','</font>','</font>','</p>'
);
return preg_replace($pattern,$replace,$message);
} private function cuturl($url){
$length = 65;
$url = substr(strtolower($url),0,4) == 'www.'?"http://$url":$url;
$urllink = "<a href=\"".$url.'"target="_blank">';
if(strlen($url)>$length){
$url = substr($url,0,intval($length*0.5)).'...'.substr($url,-intval($length*0.3));
}
$urllink .=$url.'</a>';
return $urllink;
} private function parseURL($message){
$urlPattern = "/(www.|https?:\/\/|ftp:\/\/|news:\/\/|telnet:\/\/){1}([^\[\"']+?)(com|net|org)(\/[\w-\.\/?\%\&\=]*)?/ei";
return preg_replace($urlPattern,"\$this->cuturl('\\1\\2\\3\\4')",$message);
} private function parseSmilies($message){
$pattern = array(
'/:\)|\/wx|微笑/i',
'/:@|\/fn|发怒/i',
'/:kiss|\/kill|\/sa|示爱/',
'/:p|\/tx|偷笑/i',
'/:q|\/dk|大哭/i'
);
$replace = array(
'<img src="smilies/smile.png" alt="微笑">',
'<img src="smilies/huffy.png" alt="发怒">',
'<img src="smilies/kiss.png" alt="示爱">',
'<img src="smilies/titter.png" alt="偷笑">',
'<img src="smilies/cry.png" alt="大哭">'
);
return preg_replace($pattern,$replace,$message);
} private function disableKeyWords($message){
$keywords_disable = array("非法关键字一","非法关键字二","非法关键字三");
return str_replace($keywords_disable,"**",$message);
} private function prasePHPCode($message){
$pattern = '/(<\?.*?\?>)/ise';
$replace = '"<pre style=\"background:#ddd\">".highlight_string("\\1",true)."</pre>"';
return preg_replace($pattern,$replace,$message);
} private function prasePer($message){
return '<pre>'.$message.'</pre>';
} private function nltobr($message){
return nl2br($message);
} public function getSubject(){
return '<h1 align=center>'.$this->subject.'</h1>';
} public function getMessage(){
return $this->message;
}
}
?>
结果如下:
php开发文章发布示例(正则表达式实例开发)的更多相关文章
- 【前端工具】Chrome 扩展程序的开发与发布 -- 手把手教你开发扩展程序
关于 chrome 扩展的文章,很久之前也写过一篇.清除页面广告?身为前端,自己做一款简易的chrome扩展吧. 本篇文章重在分享一些制作扩展的过程中比较重要的知识及难点. 什么是 chrome 扩展 ...
- ----转载----【前端工具】Chrome 扩展程序的开发与发布 -- 手把手教你开发扩展程序
关于 chrome 扩展的文章,很久之前也写过一篇.清除页面广告?身为前端,自己做一款简易的chrome扩展吧. 本篇文章重在分享一些制作扩展的过程中比较重要的知识及难点. 什么是 chrome 扩展 ...
- 使用JSP/Servalet技术开发新闻发布系统------动态网页开发基础
什么是动态网页? 动态网页是指在服务器端运行的程序或者网页,它们会随不同客户.不同时间,返回不同的网页. 动态网页的特点? (1).交互性:即网页会根据用户的要求和选择而动态改变和响应.采用动态网页技 ...
- Spring Boot 2.x 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统
一.概述 经过HelloWorld示例(Spring Boot 2.x 快速入门(上)HelloWorld示例)( Spring Boot 2.x 快速入门(下)HelloWorld示例详解)两篇的学 ...
- 《Ionic 2 实例开发》发布
Ionic 2系列教程集结成册,在百度阅读上架发布,名为<Ionic 2实例开发>(点击书名将打开地址:http://yuedu.baidu.com/ebook/ba1bca51e4189 ...
- 一步步开发自己的博客 .NET版(4、文章发布功能)百度编辑器
前言 这次开发的博客主要功能或特点: 第一:可以兼容各终端,特别是手机端. 第二:到时会用到大量html5,炫啊. 第三:导入博客园的精华文章,并做分类.(不要封我) 第四:做个插件,任何网站上的技术 ...
- 前端MVC Vue2学习总结(七)——ES6与Module模块化、Vue-cli脚手架搭建、开发、发布项目与综合示例
使用vue-cli可以规范项目,提高开发效率,但是使用vue-cli时需要一些ECMAScript6的知识,特别是ES6中的模块管理内容,本章先介绍ES6中的基础与模块化的内容再使用vue-cli开发 ...
- iOS开发之详解正则表达式
本文由Charles翻自raywenderlich原文:NSRegularExpression Tutorial: Getting Started更新提示:本教程被James Frost更新到了iOS ...
- 如何开发和发布一个Vue插件
前言 Vue 项目开发过程中,经常用到插件,比如原生插件 vue-router.vuex,还有 element-ui 提供的 notify.message 等等.这些插件让我们的开发变得更简单更高效. ...
随机推荐
- 《metasploit渗透测试魔鬼训练营》学习笔记第四章—web应用渗透
继续来学习metasploit...记好笔记是很重要的,下面开始正文: 二.WEB应用渗透技术 1.WEB应用渗透基础知识 先介绍WEB应用攻击的主要类型(大致介绍,具体请自行查 ...
- Spring(十)之自定义事件
编写自定义事件的简单流程如下: (1)编写CustomEvent.java package com.tutorialspoint; import org.springframework.context ...
- BigDecimal.setScale用法总结
1. BigDecimal num1 = new BigDecimal(2.225667);//这种写法不允许,会造成精度损失 2. BigDecimal num2 = new BigDecimal( ...
- JNI由浅入深_6_简单对象的应用
1.声明native方法 public class ComplexObject { /** * 返回一个对象数组 * @param val * @return */ public native Per ...
- uboot 移植 要点
1.第一 首先要 学会 shell 语法 比如 变量的 概念 变量的使用 ,if 语法 ,以及简单 IF 语法(与 或预算),以及 while for 循环 等等语法,才能看得懂 uboot ...
- quartz定时任务时间表达式说明
quartz定时任务时间表达式说明 这些星号由左到右按顺序代表 : * * * * * * * 格式: [秒] [分] [小时] [日] [月 ...
- ibatis运行的SQL语句的输出——通过配置log4j
将ibatis 的log4j运行级别调到DEBUG可以在控制台打印出ibatis运行的sql语句 ### 设置Logger输出级别和输出目的地 ###log4j.rootLogger=debug,st ...
- OO 第三次博客总结
调研规格化设计 1950年代,第一次分离,主程序和子程序的分离程序结构模型是树状模型,子程序可先于主程序编写.通过使用库函数来简化编程,实现最初的代码重用.产生基本的软件开发过程:分析—设计—编码—测 ...
- JS-instanceof 与typeof
通常来讲,使用 instanceof 就是判断一个实例是否属于某种类型 而typeof 无论引用的是什么类型的对象,它都返回 "object" var arr=[1,2,3]; ...
- BZOJ 1941: [Sdoi2010]Hide and Seek(k-d Tree)
Time Limit: 16 Sec Memory Limit: 162 MBSubmit: 1712 Solved: 932[Submit][Status][Discuss] Descripti ...