Php: Let's talk about the PHP article Release 2.

Source: Internet
Author: User
Php: Let's take a closer look at the PHP article release operations 2 and then let's take a look at how acticle_class.php works. & Lt ;? PhpclassActicle {declares an article class, which has two member attribute titles and content. if necessary, you can add more private $ subject; the article title member attribute private $ message; php: let's talk about PHP article Release 2

Then let's take a look at how the core acticle_class.php works.

 Subject = $ this-> html2Text ($ subject); // assign an initial value to the article title and convert the HTML tag to the object if (! Empty ($ parse) {// If you select the Operation option for the article, the condition is foreach ($ parse as $ value) {// switch ($ value) several times if you have selected several article Operation options {// call different internal methods to process case 1 based on different options selected by the user: // if the "delete HTML tag" option is selected, the condition is set to $ message = $ this-> delHtmlTags ($ message); break; case 2: // if the "convert HTML tag to object" option is selected, the condition is set to $ message = $ this-> html2Text ($ message); break; case 3: // if the user selects the "use UBB code" option, the condition is set to $ message = $ this-> UBBCode2Html ($ message); break; case 4: // if the condition is true when the user selects the "Enable URL recognition" oPtion $ message = $ This-> parseURL ($ message); break; case 5: // if the condition is true when the "use emoticon" option is selected $ message = $ this-> parseSmilies ($ message ); break; case 6: // if the condition is true when the "disable illegal keyword" option is selected, $ message = $ this-> disableKeyWords ($ message); break; case 7: // if the condition is set to $ message = $ this-> prasePHPCode ($ message); break; case 8: // if the condition is set to $ message = $ this-> prasePer ($ message); break; case 9: // if the condition is true when the "synchronous line feed" option is selected, $ message = $ this-> nltobr ($ message); bre Ak ;}}$ this-> message = $ message; // assign an initial value to the member attribute $ message,} private function delHtmlTags ($ message) {// This private method is used to delete the HTML tag return strip_tags ($ message); // call the string processing function to delete the HTML tag} private function html2Text ($ message) {// This private method is used to convert HTML tags into HTML entities return htmlSpecialChars (stripSlashes ($ message); // call the string processing function for operations} private function UBBCode2Html ($ message) {// This private method is used to parse UBB code $ pattern = array ('/\ [B \]/I', '/\ [\/B \]/I ', '/\ [I \]/I', // sound Template array '/\ [\/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 \-_] + [.] [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 ('','','', // Declare the replacement array of the regular expression'','','','','','','','

',' \ 2', '\ 3',' \ 1 @ \ 2', '\ 3 ','','','', '','

'); Return preg_replace ($ pattern, $ replace, $ message); // call the replacement function of the regular expression} private function cuturl ($ url) {// This private method is used to cut long URLs and add the link $ length = 65; $ urllink = "'; if (strlen ($ URL)> $ length) {// if the URL length is greater than 65, cut $ url = substr ($ url, 0, intval ($ length * 0.5 )). '... '. substr ($ url,-intval ($ length * 0.3);} $ urllink. = $ url. ''; return $ urllink;} private function parseURL ($ message) {// This private method is used to parse the URL and add the link $ urlPattern ="/(w Ww. | 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) {// This method is used to parse the expression $ pattern = array ('/: \) |\/ wx | smile/I ', // declare the regular expression template array of the expression '/: @ | \/fn | angry/I', '/: kiss | \/kill | \/sa | show love /', '/: p | \/tx | laugh/I', '/: q | \/dk | cry/I'); $ replace = array ('', // declare the replace array '',''); return preg_replace ($ pattern, $ replace, $ message ); // call the replacement function of the regular expression} private function disableKeyWord S ($ message) {// This method is used to block the illegal keyword $ keywords_disable = array ("illegal keyword 1", "illegal keyword 2", "illegal keyword 3") in the article "); return str_replace ($ keywords_disable, "**", $ message);} private function prasePHPCode ($ message) {// This method is used to set the PHP code to highlight $ pattern = '/(<\?. *? \?>) /Ise '; $ replace = '"
".highlight_string("\\1",true)."
"'; Return preg_replace ($ pattern, $ replace, $ message);} private function prasePer ($ message) {// This method is used to output the document as is, that is, add
Flag return'
'.$message.'
';} Private function nltobr ($ message) {// This private method is used to convert the line feed symbol
Mark return nl2br ($ message); // call the string processing function nl2br ()} public function getSubject () {// This method is public and returns the post title return ''. $ this-> subject. '';} public function getMessage () {// this method is public. return the body content of the article return $ this-> message ;}}?>
The main content of this class is its constructor. it traverses every item in the parse array and processes the message once.

Basically, php's built-in string processing functions are called directly. The problem is to replace them with regular expressions.

Preg_replace ($ pattern, $ replace, $ message). The first parameter represents the regular expression pattern array, and the second parameter represents the content after the regular expression is replaced. it is also an array, the two should have a one-to-one relationship.

The regular expression is basically separated by/XXXXXX/yyy in the format of/.../. yyy indicates that the matching parameter contains | or. for details, refer to the online materials or books. It is often used for user logon.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.