thinkphp HTML tag code and UBB convert each other

Source: Internet
Author: User

1.UBB to HTML

The extension of TP comes with a Ubb method that can be used to convert the user-entered UBB format code into the code of the HTML tag. The basic knowledge used here is regular expressions, and today we don't speak regular expressions.

Take a look at the method of TP, the position of this class in: \thinkphp\extend\function\extend.php, Ubb method all the code is as follows:

1 functionUbb$Text) {2   $Text=Trim($Text);3   //$Text =htmlspecialchars ($Text);4   $Text=Preg_replace("/\\t/is", "",$Text);5   $Text=Preg_replace("/\[h1\" (. +?) \[\/h1\]/is "," $Text);6   $Text=Preg_replace("/\[h2\" (. +?) \[\/h2\]/is "," $Text);7   $Text=Preg_replace("/\[h3\" (. +?) \[\/h3\]/is "," $Text);8   $Text=Preg_replace("/\[h4\" (. +?) \[\/h4\]/is "," $Text);9   $Text=Preg_replace("/\[h5\" (. +?) \[\/h5\]/is "," $Text);Ten   $Text=Preg_replace("/\[h6\" (. +?) \[\/h6\]/is "," $Text); One   $Text=Preg_replace("/\[separator\]/is", "",$Text); A   $Text=Preg_replace("/\[center\" (. +?) \[\/center\]/is "," <center>\\1</center> ",$Text); -   $Text=Preg_replace("/\[url=http:\/\/([^\[]*) \] (. +?) \[\/url\]/is "," <a href=\ "http://\\1\" target=_blank>\\2</a> ",$Text); -   $Text=Preg_replace("/\[url= ([^\[]*) \] (. +?) \[\/url\]/is "," <a href=\ "http://\\1\" target=_blank>\\2</a> ",$Text); the   $Text=Preg_replace("/\[url\]http:\/\/([^\[]*) \[\/url\]/is", "<a href=\" http://\\1\ "target=_blank>\\1</a>",$Text); -   $Text=Preg_replace("/\[url\" ([^\[]*) \[\/url\]/is "," <a href=\ "\\1\" target=_blank>\\1</a> ",$Text); -   $Text=Preg_replace("/\[img\" (. +?) \[\/img\]/is ","  ",$Text); -   $Text=Preg_replace("/\[color= (. +?) \](.+?) \[\/color\]/is "," <font color=\\1>\\2</font> ",$Text); +   $Text=Preg_replace("/\[size= (. +?) \](.+?) \[\/size\]/is "," <font size=\\1>\\2</font> ",$Text); -   $Text=Preg_replace("/\[sup\" (. +?) \[\/sup\]/is "," <sup>\\1</sup> ",$Text); +   $Text=Preg_replace("/\[sub\" (. +?) \[\/sub\]/is "," <sub>\\1</sub> ",$Text); A   $Text=Preg_replace("/\[pre\" (. +?) \[\/pre\]/is "," <pre>\\1</pre> ",$Text); at   $Text=Preg_replace("/\[email\" (. +?) \[\/email\]/is "," <a href= ' mailto:\\1 ' >\\1</a> ",$Text); -   $Text=Preg_replace("/\[colortxt\" (. +?) \[\/colortxt\]/eis "," Color_txt (' \\1 ') ",$Text); -   $Text=Preg_replace("/\[emot\" (. +?) \[\/emot\]/eis "," Emot (' \\1 ') ",$Text); -   $Text=Preg_replace("/\[i\" (. +?) \[\/i\]/is "," <i>\\1</i> ",$Text); -   $Text=Preg_replace("/\[u\" (. +?) \[\/u\]/is "," <u>\\1</u> ",$Text); -   $Text=Preg_replace("/\[b\" (. +?) \[\/b\]/is "," <b>\\1</b> ",$Text); in   $Text=Preg_replace("/\[quote\" (. +?) \[\/quote\]/is "," <div class= ' quote ' >$Text); -   $Text=Preg_replace("/\[code\" (. +?) \[\/code\]/eis "," Highlight_code (' \\1 ') ",$Text); to   $Text=Preg_replace("/\[php\" (. +?) \[\/php\]/eis "," Highlight_code (' \\1 ') ",$Text); +   $Text=Preg_replace("/\[sig\" (. +?) \[\/sig\]/is "," <div class= ' sign ' >\\1</div> ",$Text); -   $Text=Preg_replace("/\\n/is", "<br/>",$Text); the   return $Text; *}

The function is to convert the UBB code inside the string containing the UBB code into the HTML tag code, returning the converted string.

Maybe because I am more rookie, so I have a little attention to a small detail, here to remind all the same as my rookie, is in the thinkphp, there is a concept called " pattern modifier ", is

$Text =preg_replace ("/\[php\" (. +?) \[\/php\]/eis"," Highlight_code (' \\1 ') ", $Text);

The Red callout section, see details: pattern modifier

So in the thinkphp backstage with preg_replace (pattern, replacement, subject) method for regular replacement, need to pay attention to, parameter pattern inside need "//" Separate the regular and pattern modifiers, and the pattern modifier to the right of the next "/". The true regular expression is in the middle of two "/".

2.HTML converted to UBB

Because the site uses UBB code is to improve security, and UBB code after writing only for browsing, very few will be written to convert HTML to UBB, even if there is more JS code to see, below I share a method I found on the internet, basically feasible.

Of course, if you write in the background with PHP is completely possible, given my regular expression is not proficient, here is the first to give the JS version.

1     functionHtmltoubb (str) {2str = str.replace (/<br[^>]*>/ig, ' \ n '));3str = str.replace (/<p[^>\/]*\/>/ig, ' \ n ')); 4str = str.replace (/\son[\w]{3,16}\s?=\s* ([\ ' \ "]). +?\1/ig,");5str = str.replace (/);6str = str.replace (/< (sub|sup|u|strike|b|i|pre) >/ig, ' [$] ');7str = str.replace (/<\/(sub|sup|u|strike|b|i|pre) >/ig, ' [/$1] ');8str = str.replace (/< (\/) B>/ig, ' [$1b] ');9str = str.replace (/< (\/) Em>/ig, ' [$1i] ');Tenstr = str.replace (/< (/)? BLOCKQUOTE ([^>]*) >/ig, ' [$1blockquote] '); Onestr = str.replace (/]*smile=\ "(\d+) \" [^>]*>/ig, ' [s:$1] '); Astr = str.replace (/]*src=[\ ' \ "\s]* ([^\s\ ' \"]+) [^>]*>/ig, ' [img] ' + ' + ' + ' [/img] '); -  -str = str.replace (/<a[^>]*href=[\ ' \ "\s]* ([^\s\ ' \"]*) [^>]*> (. +?) <\/a>/ig, ' [url] ' + ' + ' + ' [/url] '); thestr = str.replace (/

); -str = str.replace (/<[^>]*?>/ig, "); -str = str.replace (/&amp;/ig, ' & '); -str = str.replace (/&lt;/ig, ' < '); +str = str.replace (/&gt;/ig, ' > '); -}

The use of this is to pass in a string containing HTML tag code, the method handles the HTML tag to UBB code, returns the converted string.

Because the UBB code is limited, in the major sites used are not the same, so the corresponding conversion method contains a different, if there is not written, I think about it.

In fact, the conversion of HTML code to UBB I personally think that is not too important, the data can be completely from the user input UBB code directly saved to the database, need to display the time to take out to convert to HTML, so even if it involves the editing function can hold. You don't have to convert the UBB into HTML into the database for the front desk, and then edit it again from HTML to UBB for the user to continue editing.

The reason why I want to write HTML into UBB class is to see if I can do it, and for this I have learned the regular expression, about regular expression next time.

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.