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/imghwm/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/imghwm/default1.png" data-src="smilies/smile.gif" class="lazy" alt="微笑">', //声明表情的替换数组 '<img src="/static/imghwm/default1.png" data-src="smilies/huffy.gif" class="lazy" alt="发怒">', '<img src="/static/imghwm/default1.png" data-src="smilies/kiss.gif" class="lazy" alt="示爱">', '<img src="/static/imghwm/default1.png" data-src="smilies/titter.gif" class="lazy" alt="偷笑">', '<img src="/static/imghwm/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).""'; return preg_replace($pattern, $replace, $message); } private function prasePer($message) { //此方法用来将文章原样输出,即加上
标记 return '<pre class="brush:php;toolbar:false">'.$message.''; } 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表示匹配参数 中间有|表示或,具体可以自己参考网上资料或者书。常常用在我们的用户登录上。

In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.

CSP is important because it can prevent XSS attacks and limit resource loading, improving website security. 1.CSP is part of HTTP response headers, limiting malicious behavior through strict policies. 2. The basic usage is to only allow loading resources from the same origin. 3. Advanced usage can set more fine-grained strategies, such as allowing specific domain names to load scripts and styles. 4. Use Content-Security-Policy-Report-Only header to debug and optimize CSP policies.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

HTTPS is a protocol that adds a security layer on the basis of HTTP, which mainly protects user privacy and data security through encrypted data. Its working principles include TLS handshake, certificate verification and encrypted communication. When implementing HTTPS, you need to pay attention to certificate management, performance impact and mixed content issues.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.