Phpubb code parser-PHP Tutorial

Source: Internet
Author: User
Phpubb code parser. This article will share with you a simple article about how to use php to parse ubb code. We mainly use the preg_replace () function, with everything else, we can do the following code: Copy the code ** + article to share with you a simple article on how to use php to parse ubb code. We mainly use the preg_replace () function, with everything else
The code is as follows:

/**
+ ----------------------------------------------------------
* UBB parsing
+ ----------------------------------------------------------
* @ Return string
+ ----------------------------------------------------------
*/
Function ubb ($ Text ){
$ Text = trim ($ Text );
$ Text = ereg_replace ("n ","
", $ Text );
$ Text = preg_replace ("/\ t/is", "", $ Text );
$ Text = preg_replace ("/[hr]/is", "", $ Text );
$ Text = preg_replace ("/[separator]/is ","
", $ Text );
$ Text = preg_replace ("/[h1] (. + ?) [/H1]/is "," \ 1 ", $ Text );
$ Text = preg_replace ("/[h2] (. + ?) [/H2]/is "," \ 1 ", $ Text );
$ Text = preg_replace ("/[h3] (. + ?) [/H3]/is "," \ 1 ", $ Text );
$ Text = preg_replace ("/[h4] (. + ?) [/H4]/is "," \ 1 ", $ Text );
$ Text = preg_replace ("/[h5] (. + ?) [/H5]/is "," \ 1 ", $ Text );
$ Text = preg_replace ("/[h6] (. + ?) [/H6]/is "," \ 1 ", $ Text );
$ Text = preg_replace ("/[center] (. + ?) [/Center]/is ","

\ 1 ", $ Text );
// $ Text = preg_replace ("/[url = ([^ [] *)] (. + ?) [/Url]/is "," \ 2 ", $ Text );
$ Text = preg_replace ("/[url] (. + ?) [/Url]/is "," \ 1 ", $ Text );
$ Text = preg_replace ("/[url = (http: //. ++?)] (. + ?) [/Url]/is "," \ 2 ", $ Text );
$ Text = preg_replace ("/[url = (. +?)] (. + ?) [/Url]/is "," \ 2 ", $ Text );
$ Text = preg_replace ("/[img] (. + ?) [/Img]/is "," ", $ Text );
$ Text = preg_replace ("/[imgs (. +?)] (. + ?) [/Img]/is "," ", $ Text );
$ Text = preg_replace ("/[color = (. +?)] (. + ?) [/Color]/is "," \ 2 ", $ Text );
$ Text = preg_replace ("/[colorTxt] (. + ?) [/ColorTxt]/eis "," color_txt ('\ 1') ", $ Text );
$ Text = preg_replace ("/[style = (. +?)] (. + ?) [/Style]/is ","

\ 2

", $ Text );
$ Text = preg_replace ("/[size = (. +?)] (. + ?) [/Size]/is "," \ 2 ", $ Text );
$ Text = preg_replace ("/[sup] (. + ?) [/Sup]/is "," \ 1 ", $ Text );
$ Text = preg_replace ("/[sub] (. + ?) [/Sub]/is "," \ 1 ", $ Text );
$ Text = preg_replace ("/[pre] (. + ?) [/Pre]/is ","
\1
", $ Text );
$ Text = preg_replace ("/[emot] (. + ?) [/Emot]/eis "," emot ('\ 1') ", $ Text );
$ Text = preg_replace ("/[email] (. + ?) [/Email]/is "," \ 1 ", $ Text );
$ Text = preg_replace ("/[I] (. + ?) [/I]/is ","\ 1", $ Text );
$ Text = preg_replace ("/[u] (. + ?) [/U]/is ","\ 1", $ Text );
$ Text = preg_replace ("/[B] (. + ?) [/B]/is ","\ 1", $ Text );
$ Text = preg_replace ("/[quote] (. + ?) [/Quote]/is ","
Reference:

\ 1

", $ Text );
$ Text = preg_replace ("/[code] (. + ?) [/Code]/eis "," highlight_code ('\ 1') ", $ Text );
$ Text = preg_replace ("/[php] (. + ?) [/Php]/eis "," highlight_code ('\ 1') ", $ Text );
$ Text = preg_replace ("/[sig] (. + ?) [/Sig]/is ","



--------------------------
\ 1
--------------------------

", $ Text );
Return $ Text;
}

Preg_replace () function syntax

Preg_replace function basics and instance code
// Preg_replace (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1 [, int & $ count]) subject: match search mode, replace
/*
The pattern to be searched. It can be a string or a string array.

After the electronic modifier causes the preg_replace function () to replace the treatment, the appropriate reference as a parameter is replaced by the php Tutorial code. Tip: Make sure that the replacement constitutes a valid php code string. otherwise, php will complain about parsing errors including the preg_replace function line.

Return value

The preg_replace function () returns an array. if the parameter in this issue is an array or a string, otherwise.

If a match is found, a new problem will occur. Otherwise, the returned topic will remain unchanged or null if an error occurs.


For more details, see http://www.bKjia. c0m/phper/php-function/32.16.htm

Except () function. with everything else, you can do the following code:/** +...

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.