php开发文章发布示例(正则表达式实例开发)详解编程语言

存档:

post.php

 1 <form method="post" action="viewthread.php" target="_blank"> 
 2     <h2 align="center">发表文章实例开发</h2> 
 3     <div style="width:200;float:left"> 
 4         <h5>选项</h5> 
 5         <ul style="list-style:none;margin:0px;padding:0px"> 
 6             <li><input type="checkbox" name="parse[]" value="1">删除HTML标签</li> 
 7             <li><input type="checkbox" name="parse[]" value="2">转换HTML标签为实体</li> 
 8             <li><input type="checkbox" name="parse[]" value="3">使用UBB代码</li> 
 9             <li><input type="checkbox" name="parse[]" value="4">开启URL识别</li> 
10             <li title="可用的表情: 
11                                 【:),/wx,微笑】【:@,/fn,发怒】 
12                                 【:kiss,/kill,/sa,示爱】 
13                                 【:p,/tx,偷笑】【:q,/dk,大哭】"> 
14                 <input type="checkbox" name="parse[]" value="5">使用表情</li> 
15             <li><input type="checkbox" name="parse[]" value="6">禁用非法关键字</li> 
16             <li><input type="checkbox" name="parse[]" value="7">PHP代码设为高亮</li> 
17             <li><input type="checkbox" name="parse[]" value="8">原样显示</li> 
18             <li><input type="checkbox" name="parse[]" value="9">同步换行</li> 
19         </ul> 
20     </div> 
21     <div style="width:300;float:left"> 
22         <h5>标题<input type="text" name="subject" size=50></h5> 
23         <h5>内容<textarea rows="7" cols="50" name="message"></textarea></h5> 
24         <input type="submit" name="replysubmit" value="发表帖子"> 
25     </div> 
26 </form>

viewthread.php

1 <?php 
2     require "article.class.php"; 
3     $article = new Article($_POST["subject"],$_POST["message"],$_POST["parse"]); 
4     echo $article->getSubject(); 
5     echo "<hr>"; 
6     echo $article->getMessage(); 
7 ?>

article.class.php

  1 <?php 
  2     class Article{ 
  3         private $subject; 
  4         private $message; 
  5         function __construct($subject="",$message="",$parse=array()){ 
  6             $this->subject = $this->html2Text($subject); 
  7             if(!empty($parse)){ 
  8                 foreach($parse as $value){ 
  9                     switch($value){ 
 10                         case 1: 
 11                             $message = $this->delHtmlTags($message); 
 12                             break; 
 13                         case 2: 
 14                             $message = $this->html2Text($message); 
 15                             break; 
 16                         case 3: 
 17                             $message = $this->UBBCode2Html($message); 
 18                             break; 
 19                         case 4: 
 20                             $message = $this->parseURL($message); 
 21                             break; 
 22                         case 5: 
 23                             $message = $this->parseSmilies($message); 
 24                             break; 
 25                         case 6: 
 26                             $message = $this->disableKeyWords($message); 
 27                             break; 
 28                         case 7: 
 29                             $message = $this->prasePHPCode($message); 
 30                             break; 
 31                         case 8: 
 32                             $message = $this->prasePer($message); 
 33                             break; 
 34                         case 9: 
 35                             $message = $this->nltobr($message); 
 36                             break; 
 37                     } 
 38                 } 
 39             } 
 40             $this->message = $message; 
 41         } 
 42          
 43         private function delHtmlTags($message){ 
 44             return strip_tags($message); 
 45         } 
 46          
 47         private function html2Text($message){ 
 48             return htmlspecialchars(stripslashes($message)); 
 49         } 
 50          
 51         private function UBBCode2Html($message){ 
 52             $pattern = array( 
 53                 '//[b/]/i','//[//b/]/i','//[i/]/i', 
 54                 '//[//i/]/i','//[u/]/i','//[//u/]/i', 
 55                 '//[font=([^/[/<]+?)/]/i', 
 56                 '//[color=([#/w]+?)/]/i', 
 57                 '//[size=(/d+?)/]/i', 
 58                 '//[size=(/d+(/./d+)?(px|pt|in|cm|mm|pc|em|ex|%)+?)/]/i', 
 59                 '//[align=(left|center|right)/]/i', 
 60                 '//[url=www.([^/["/']+?)/](.+?)/[//url/]/is', 
 61                 '//[url=(https?|ftp|gopher|news|telnet){1}:////([^/["/']+?)/](.+?)/[//url]/is', 
 62                 '//[email/]/s*([a-z0-9/-_.+]+)@([a-z0-9/-_.]+)/s*/[//email/]/i', 
 63                 '//[email=([a-z0-9/-_.+]+)@([a-z0-9/-_]+[.][a-z0-9/-_.]+)/](.+?)/[//email/]/is', 
 64                 '//[img/](.+?)/[//img/]/', 
 65                 '//[//color/]/i', 
 66                 '//[//size/]/i', 
 67                 '//[//font/]/i', 
 68                 '//[//align/]/' 
 69             ); 
 70              
 71             $replace = array( 
 72                 '<b>','</b>','<i>', 
 73                 '</i>','<u>','</u>', 
 74                 '<font face="//1">', 
 75                 '<font color="//1">', 
 76                 '<font size="//1">', 
 77                 '<font style=/"font-size://1/">', 
 78                 '<p align="//1">', 
 79                 '<a href="http://www.//1" target="_blank">//2</a>', 
 80                 '<a href="//1:////2" target="_blank">//3</a>', 
 81                 '<a href="mailto://1@//2">//1@//2</a>', 
 82                 '<a href="mailto://1@//2">//3</a>', 
 83                 '<img src="//1">', 
 84                 '</font>','</font>','</font>','</p>' 
 85             ); 
 86             return preg_replace($pattern,$replace,$message); 
 87         } 
 88          
 89         private function cuturl($url){ 
 90             $length = 65; 
 91             $url = substr(strtolower($url),0,4) == 'www.'?"http://$url":$url; 
 92             $urllink = "<a href=/"".$url.'"target="_blank">'; 
 93             if(strlen($url)>$length){ 
 94                 $url = substr($url,0,intval($length*0.5)).'...'.substr($url,-intval($length*0.3)); 
 95             } 
 96             $urllink .=$url.'</a>'; 
 97             return $urllink; 
 98         } 
 99          
100         private function parseURL($message){ 
101             $urlPattern = "/(www.|https?:////|ftp:////|news:////|telnet:////){1}([^/[/"']+?)(com|net|org)(//[/w-/.//?/%/&/=]*)?/ei"; 
102             return preg_replace($urlPattern,"/$this->cuturl('//1//2//3//4')",$message); 
103         } 
104          
105         private function parseSmilies($message){ 
106             $pattern = array( 
107                 '/:/)|//wx|微笑/i', 
108                 '/:@|//fn|发怒/i', 
109                 '/:kiss|//kill|//sa|示爱/', 
110                 '/:p|//tx|偷笑/i', 
111                 '/:q|//dk|大哭/i' 
112             ); 
113             $replace = array( 
114                 '<img src="smilies/smile.png" alt="微笑">', 
115                 '<img src="smilies/huffy.png" alt="发怒">', 
116                 '<img src="smilies/kiss.png" alt="示爱">', 
117                 '<img src="smilies/titter.png" alt="偷笑">', 
118                 '<img src="smilies/cry.png" alt="大哭">' 
119             ); 
120             return preg_replace($pattern,$replace,$message); 
121         } 
122          
123         private function disableKeyWords($message){ 
124             $keywords_disable = array("非法关键字一","非法关键字二","非法关键字三"); 
125             return str_replace($keywords_disable,"**",$message); 
126         } 
127          
128         private function prasePHPCode($message){ 
129             $pattern = '/(</?.*?/?>)/ise'; 
130             $replace = '"<pre style=/"background:#ddd/">".highlight_string("//1",true)."

“‘;
131 return preg_replace($pattern,$replace,$message);
132 }
133
134 private function prasePer($message){
135 return

'.$message.'

;
136 }
137
138 private function nltobr($message){
139 return nl2br($message);
140 }
141
142 public function getSubject(){
143 return ‘<h1 align=center>’.$this->subject.'</h1>’;
144 }
145
146 public function getMessage(){
147 return $this->message;
148 }
149 }
150 ?>

结果如下:

php开发文章发布示例(正则表达式实例开发)详解编程语言

 

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/11938.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论