Table of Contents
'.$this->subject.'
Home Backend Development PHP Tutorial php:细说PHP之稿子发布操作实2

php:细说PHP之稿子发布操作实2

Jun 13, 2016 am 11:58 AM
gt lt message quot

php:细说PHP之文章发布操作实2

然后我们来看看,最核心的acticle_class.php怎么运作。

<?php class Acticle {   //声明一个文章类,其中有两个成员属性标题和内容,如果需要还可以更多			private $subject;        //文章的标题成员属性		private $message;       //文章的主本内容成员属性				//构造方法,通过传入文章标题和文章主体和文章的操作选项数组创建文章对象		function __construct($subject=" ",$message=" ", $parse=array()) {			$this->subject=$this->html2Text($subject); //为文章标题赋初值,将HTML标记转为实体              			if(!empty($parse)) {            //如果用户选择了对文章的操作选项则条件成功				foreach($parse as $value) {  //用户选择了几个文章操作选项则循环几次					switch($value) {      //根据用户选择的不同选项,调用不同的内部方法处理						case 1:         //如果用户选择“删除HTML标签”选项时条件成立							$message=$this->delHtmlTags($message);  							break;						case 2:        //如果选择“转换HTML标签为实体”选项时条件成立							$message=$this->html2Text($message);							break;							case 3:        //如果用户选择“使用UBB代码”选项时条件成立							$message=$this->UBBCode2Html($message);							break;						case 4:        //如果用户选择“开启URL识别”选项时条件成立							$message=$this->parseURL($message);							break;						case 5:        //如果用户选择“使用表情”选项时条件成立							$message=$this->parseSmilies($message);							break;						case 6:        //如果用户选择“禁用非法关键字”选项时条件成立							$message=$this->disableKeyWords($message);							break;						case 7:        //如果用户选择“PHP代码设为高亮”选项时条件成立							$message=$this->prasePHPCode($message);							break;						case 8:        //如果用户选择“原样显示”选项时条件成立							$message=$this->prasePer($message);							break;						case 9:        //如果用户选择“同步换行”选项时条件成立							$message=$this->nltobr($message);							break;					}				}				}				$this->message=$message;         //给成员属性$message赋初值,		}				private function delHtmlTags($message) {  //此私有方法有来删除HTML标记			return strip_tags($message);         //调用字符串处理函数删除HTML标记		}		private function html2Text($message) {   //此私有方法有来将HTML标记转为HTML实体			return htmlSpecialChars(stripSlashes($message));  //调用字符串处理函数进行操作		}		private function UBBCode2Html($message) {   //此私有方法有来解析UBB代码			$pattern=array('/\[b\]/i', '/\[\/b\]/i', '/\[i\]/i',   //声明正则表达式的模板数组			       	'/\[\/i\]/i', '/\[u\]/i', '/\[\/u\]/i',              				'/\[font=([^\[\', '', '<i>',    //声明正则表达式的替换数组				'</i>', '<u>', '</u>',                     				'<font face="\\1">',                           				'<font color="\\1">',                         				'<font size="\\1">',                          				'<font style='\"font-size:'>',                 				'<p align="\\1">',                             				'<a href="http://www.%5C%5C1" target="_blank">\\2</a>', 				'<a href="%5C%5C1://%5C%5C2" target="_blank">\\3</a>',       				'<a href="mailto:%5C%5C1@%5C%5C2">\\1@\\2</a>',          				'<a href="mailto:%5C%5C1@%5C%5C2">\\3</a>',              				'<img  src="/static/imghw/default1.png" data-src="\\1" class="lazy" alt="php:细说PHP之稿子发布操作实2" >',                            				'</p></font>', '</font>', '</font>', ''             			);			return preg_replace($pattern, $replace, $message);  //调用正则表达式的替换函数		}		private function cuturl($url) {      //此私有方法用来剪切长的URL,并加上链接			$length = 65;			$urllink = "<a href="%5C%22%22.(substr(strtolower(%24url)," : target="_blank">';			if(strlen($url) > $length) {    //如果URL长度大于65则剪切				$url = substr($url, 0, intval($length * 0.5)).' ... '.substr($url, - intval($length * 0.3));			}			$urllink .= $url.'</a>';			return $urllink;		}		private function parseURL($message) {   //此私有方法用来解析URL,将其加上链接$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="/static/imghw/default1.png" data-src="smilies/smile.gif" class="lazy" alt="微笑">',  //声明表情的替换数组					'<img src="/static/imghw/default1.png" data-src="smilies/huffy.gif" class="lazy" alt="发怒">',					'<img src="/static/imghw/default1.png" data-src="smilies/kiss.gif" class="lazy" alt="示爱">',					'<img src="/static/imghw/default1.png" data-src="smilies/titter.gif" class="lazy" alt="偷笑">',					'<img src="/static/imghw/default1.png" data-src="smilies/cry.gif" class="lazy" alt="大哭">');			return preg_replace($pattern, $replace, $message);	     //调用正则表达式的替换函数		}				private function disableKeyWords($message) {   //此方法用来屏蔽文章中的非法关键字			$keywords_disable=array("非法关键字一","非法关键字二","非法关键字三");			return str_replace($keywords_disable,"**",$message);		}		private function prasePHPCode($message) {    //此方法用来将PHP代码设置为高亮			$pattern='/()/ise';			$replace='"<pre   style="max-width:90%"background:#ddd\"'>".highlight_string("\\1",true)."
Copy after login
"'; return preg_replace($pattern, $replace, $message); } private function prasePer($message) { //此方法用来将文章原样输出,即加上
标记			return '<pre class="brush:php;toolbar:false">'.$message.'
Copy after login
'; } private function nltobr($message) { //此私有方法用来将换行符号转为
标记 return nl2br($message); //调用字符串处理函数nl2br() } public function getSubject() { //此方法为公有的,返回文章的标题 return '

'.$this->subject.'

'; } public function getMessage() { //此方法为公有的,返回文章的主体内容 return $this->message; } }?>该类主要内容就是它的构造方法,遍历parse数组里面的每一项,对message做一次处理。

基本上都是直接调用php自带的字符串的处理函数,还有问题就是利用正则表达式替换。

preg_replace($pattern, $replace, $message),第一个参数表示正则表达式模式数组,第二个参数表示将这个遇到正则表达式替换后的内容,也是数组,两者应该是一一对应的。

正则表达式基本上就是/XXXXXX /yyy 以/... /表示分隔,yyy表示匹配参数 中间有|表示或,具体可以自己参考网上资料或者书。常常用在我们的用户登录上。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

How to use element-plus to call message in vue3 How to use element-plus to call message in vue3 May 17, 2023 pm 03:52 PM

vue3 uses element-plus to call the message environment: vue3+typescript+element-plus1. After the global introduction of element, element has added the global method $message in app.config.globalProperties, so you can use mounted(){(thisasany) directly in the optionsAPI. $message.success("this.$message");}2. In the compositionAPI, the setup method passes in two variables props and

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

Is watch4pro better or gt? Is watch4pro better or gt? Sep 26, 2023 pm 02:45 PM

Watch4pro and gt each have different features and applicable scenarios. If you focus on comprehensive functions, high performance and stylish appearance, and are willing to bear a higher price, then Watch 4 Pro may be more suitable. If you don’t have high functional requirements and pay more attention to battery life and reasonable price, then the GT series may be more suitable. The final choice should be decided based on personal needs, budget and preferences. It is recommended to carefully consider your own needs before purchasing and refer to the reviews and comparisons of various products to make a more informed choice.

How to optimize iPad battery life with iPadOS 17.4 How to optimize iPad battery life with iPadOS 17.4 Mar 21, 2024 pm 10:31 PM

How to Optimize iPad Battery Life with iPadOS 17.4 Extending battery life is key to the mobile device experience, and the iPad is a good example. If you feel like your iPad's battery is draining too quickly, don't worry, there are a number of tricks and tweaks in iPadOS 17.4 that can significantly extend the run time of your device. The goal of this in-depth guide is not just to provide information, but to change the way you use your iPad, enhance your overall battery management, and ensure you can rely on your device for longer without having to charge it. By adopting the practices outlined here, you take a step toward more efficient and mindful use of technology that is tailored to your individual needs and usage patterns. Identify major energy consumers

请教怎么修改url某一参数的参数值呢?是要拆开了再拼回去吗 请教怎么修改url某一参数的参数值呢?是要拆开了再拼回去吗 Jun 13, 2016 am 10:24 AM

请问如何修改url某一参数的参数值呢?是要拆开了再拼回去吗?那么请问如何修改url某一参数的参数值呢?是要拆开了再拼回去吗?http://127.0.0.1/myo/newuser.php?mod=search&type=fastone比如现在我要修改mod=new要怎么做呢?------解决方案--------------------发送了请求

See all articles