Develop your own UBB code in PHP learning notes (36)

Source: Internet
Author: User

What is UBB?Code

The UBB code is a variant of HTML and is the ultimate Bulletin Board (a foreign BBS Program , There are many places in China that use this program) using a special tag. The UBB code is very simple. Although there are few functions, it basically implements some of our commonly used functions. some codes that can be defined by yourself in the PHP file can then be implemented in HTML based on these codes. Advantages Compared with HTML, it is highly secure and easy to use, making it easier to customize memory features and better scalability (you can define it based on your needs, but it is generally best to use a general standard, such as URL and IMG) effectively reduces database storage code Disadvantages Weak typographical functions cannot be used in combination.

UBB code running process UBB code-> resolution conversion-> HTML code is actually replaced by the replacement function of the PHP regular expression, obtain the UBB Code UBB-> DB-> resolution-> output UBB-> resolution-> DB-> output Several Simple UBB Code UBB regular HTML

[Url] [/url]/(\ [url \]) (. *) (\ [\/URL \])/I <a href =

[B] [/B]/(\ [B \]) (. *) (\ [\/B \])/I <B>

[Img] [/img]/(\ [img \]) (. *) (\ [\/IMG \])/I Write your own UBB

<? Phpfunction get_ubb ($ Str) {$ STR = preg_replace ("/(\ [) EM (.*?) (\])/I ","  ", $ Str ); // link UBB $ STR = preg_replace ("/(\ [url \]) (. *) (\ [\/URL \])/I "," <a href = \ 2 target = \ "New \" >\\ 2 </a> ", $ Str); // QQ number UBB $ STR = preg_replace ("// \ [QQ \] ([0-9] *) \ [\/QQ \]/I ", "<a target = \" _ blank \ "href = \" Tencent: // message /? Uin =\\ {1} & Site = www.php100.com & menu = Yes \ ">  </ a> ", $ Str); // link $ STR = preg_replace ("/(\ [B \]) (. *) (\ [\/B \])/I "," <B >\\ 2 </B> ", $ Str); // bold return $ STR ;} if ($ _ post ['sub']) {echo get_ubb ($ _ post [Message]) ;}?> <SCRIPT> function inserttag (topen, tclose) {var themess = document. getelementbyid ('Con '); // edit the themess object. focus (); If (document. selection) {// If the IE browser var theselection = document. selection. createRange (). text; // obtain the selected area text // alert (theselection); If (theselection) {document. selection. createRange (). TEXT = theselection = topen + theselection + tclose; // replace} else {document. selection. createRange (). TEXT = topen + tclose;} theselection = '';} else {// other browsers var scrollpos = themess. scrolltop; var sellength = themess. textlength; var selstart = themess. selectionstart; // index of the start point of the constituency. 0 var selend = themess is not selected. selectionend; // If (selend <= 2) selend = sellength; var S1 = (themess. value ). substring (0, selstart); // truncates the first part of the character var S2 = (themess. value ). substring (selstart, selend) // capture and select part of the character var S3 = (themess. value ). substring (selend, sellength); // truncate the themess. value = S1 + topen + S2 + tclose + S3; // replace themess. focus (); themess. selectionstart = Newstart; themess. selectionend = Newstart; themess. scrolltop = scrollpos; return ;}} </SCRIPT> <HR/> <font size = 2>         <a href = "javascript: void (0); "onclick = 'inserttag (" [B] "," [/B] "); '> bold </a> <a href =" javascript: void (0); "onclick = 'inserttag (" [QQ] "," [/QQ] "); '> QQ number </a> <a href =" javascript: void (0); "onclick = 'inserttag (" [url] "," [/url] "); '> hyperlink </a> <br> </font> <form action = "" method = "Post"> <textarea name = "message" id = "con" Cols = "70%" rows = "10"> </textarea> <br> <input type = "Submit" name = "sub" value = "Submit"/> </form>

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.