PHP convert HTML code to UBB Forum Code _php Tutorial

Source: Internet
Author: User
Strong -Fire -NET (Liehuo -. Net) teaches -Cheng recently to download the column to add a key reprint function, fire download column is the use of dedecms construction, we all know Dede not a key reprint function, so had to write a, and finally realized. Here is the HTML transcoding UBB code for the software introduction.

One-click Reprint Demo:

About PHP to the HTML to UBB code problems, the author found on the Internet several, but are not very useful, so they have to modify and organize, if you have other needs, you can leave a message below.

PHP Code:

Copy to ClipboardWhat to refer to: [www.bkjia.com] $intro = $liehuo-net; Here is the value of the variable you get.
$intro = Str_replace ("\ n", "", $intro);
$intro = Preg_replace ("/\]+href=\" ([^\ "]+) \" [^>]*\> (. *?) <\/a\>/i "," [Url=$1]$2[/url] ", $intro);
$intro = Preg_replace ("/\]+) \" (. *?) \> (. *?) <\/font>/i "," [Color=$2]$4[/color] ", $intro);
$intro = Preg_replace ("/\]+) \" (. *?) \> (. *?) <\/font>/i "," [Face=$2]$4[/face] ", $intro);
$intro = Preg_replace ("/\]+) \" (. *?) \> (. *?) <\/font>/i "," [Size=$2]$4[/size] ", $intro);
$intro = Preg_replace ("/\]+src=\" ([^\ "]+) \" [^>]*\>/i "," [Img]$1[/img] ", $intro);
$intro = Preg_replace ("/\]+align=\" ([^\ "]+) \" [^>]*\> (. *?) <\/div\>/i "," [Align=$1]$2[/align] ", $intro);
$intro = Preg_replace ("/\ ]+align=\ "([^\"]+) \ "[^>]*\> (. *?) <\/p\>/i "," [Align=$1]$2[/align] ", $intro);
$intro = Preg_replace ("/\<" ([\/]?) U\>/i "," [$1u] ", $intro);
$intro = Preg_replace ("/\<" ([\/]?) Em\>/i "," [$1i] ", $intro);
$intro = Preg_replace ("/\<" ([\/]?) Strong\>/i "," [$1b] ", $intro);
$intro = Preg_replace ("/\<" ([\/]?) I\>/i "," [$1i] ", $intro);
Echo $intro; Finally get UBB code
?>

Now that we know the conversion of HTML to UBB, can ubb return HTML? Please refer to the following code, collected online.

Code One:

Copy to ClipboardWhat to refer to: [www.bkjia.com] function ubb2html ($strCodes) {
$match = Array (
"%\[b\" (. *?) \[\/b\]%si ",
"%\[center\" (. *?) \[\/center\]%si ",
"%\[url\" (. *?) \[\/url\]%si ",
"%\[url= (. *?) \](.*?) \[\/url\]%si ",
"%\[img\" (. *?) \[\/img\]%si ",
"%\[fieldset= (. *?) \](.*?) \[\/fieldset\]%si "

);
$replace = Array (
" $",
" $ ",
"$",
"$",
"550" this.width=500\ "title=\" Fire tips: Click to view \ ">",
"
$ $ "
);
$strCodes = Preg_replace ($match, $replace, $strCodes);

return $strCodes;
}
?>

Code two:

Copy to ClipboardWhat to refer to: [www.bkjia.com] function ubb2html ($sUBB)
{
$sHtml = $sUBB;
$sHtml =preg_replace ("/ $sHtml =preg_replace ("/>/", ' > ', $sHtml);
$sHtml =preg_replace ("/\r?\n/", '
', $sHtml);
$sHtml =preg_replace ("/\[(\ \)" (b|u|i|s|sup|sub) \]/i ", ' <$1$2> ', $sHtml);
$sHtml =preg_replace ("/\[color\s*=\s*" ([^\]]+?) \]/i ", ' ', $sHtml);
function Getsizename ($match)
{
$arrSize =array (' 8pt ', ' 10pt ', ' 12pt ', ' 14pt ', ' 18pt ', ' 24pt ', ' 36pt ');
Return '';
}
$sHtml =preg_replace_callback ("/\[size\s*=\s* (\d+?) \]/i ", ' Getsizename ', $sHtml);
$sHtml =preg_replace ("/\[font\s*=\s*" ([^\]]+?) \]/i ", '', $sHtml);
$sHtml =preg_replace ("/\[back\s*=\s*" ([^\]]+?) \]/i ", '', $sHtml);
$sHtml =preg_replace ("/\[\/(color|size|font|back) \]/i", '', $sHtml);

for ($i =0; $i <3; $i + +) $sHtml =preg_replace ("/\[align\s*=\s* ([^\]]+?) \](((?! \[align (?: \ s+[^\]]+) [\s\s]) *?) \[\/align\]/", '

$

', $sHtml);
$sHtml =preg_replace ("/\[img\]\s*" ([\s\s]+?) \s*\[\/img\]/i ",", $sHtml);
function Getimg ($match)
{
$p 1= $match [1]; $p 2= $match [2]; $p 3= $match [3]; $src = $match [4]; Title= "Fire tip: Click to view"
$a = $p 3? $p 3: ($p 2? $p 1: ");
Return ';
}
$sHtml =preg_replace_callback ("/\[img\s*= (?: \ \ s* (\d+) \s*,\s* (\d+) \s*)? (?:,? \s* (\w+) \s*)? \]\s* ([\s\s]+?) \s*\[\/img\]/i ", ' getimg ', $sHtml);
$sHtml =preg_replace ("/\[url\]\s*" ([\s\s]+?) \s*\[\/url\]/i ", ' $ $ ', $sHtml);
$sHtml =preg_replace ("/\[url\s*=\s*" ([^\]\s]+?) \s*\]\s* ([\s\s]+?) \s*\[\/url\]/i ", ' $ $ ', $sHtml);
$sHtml =preg_replace ("/\[email\]\s*" ([\s\s]+?) \s*\[\/email\]/i ", ' $ $ ', $sHtml);
$sHtml =preg_replace ("/\[email\s*=\s*" ([^\]\s]+?) \s*\]\s* ([\s\s]+?) \s*\[\/email\]/i ", ' $ $ ', $sHtml);
$sHtml =preg_replace ("/\[quote\" ([\s\s]*?) \[\/quote\]/i ", '
$
', $sHtml);
$sHtml =preg_replace ("/\[code\" ([\s\s]*?) \[\/code\]/i ", ' $1', $sHtml);
function Getflash ($match)
{
$w = $match [1]; $h = $match [2]; $url = $match [3];
if (! $w) $w =550;if (! $h) $h = 400;
Return '';
}
$sHtml =preg_replace_callback ("/\[flash\s*" (?: =\s* (\d+) \s*,\s* (\d+) \s*)? \] ([\s\s]+?) \[\/flash\]/i ", ' Getflash ', $sHtml);
function Getmedia ($match)
{
$w = $match [1]; $h = $match [2]; $play = $match [3]; $url = $match [4];
if (! $w) $w =550;if (! $h) $h = 400;
Return '';
}
$sHtml =preg_replace_callback ("/\[media\s* (?: =\s* (\d+) \s*,\s* (\d+) \s* (?:, \s* (\d+) \s*)?" \] ([\s\s]+?) \[\/media\]/i ", ' Getmedia ', $sHtml);
function getTable ($match)
{
$w = $match [1]; $b = $match [2];
$str = '
if ($w) $str. = ' width= '. $w. ' ";
if ($b) $str. = ' bgcolor= '. $b. ' ";
return $str. ' > ';
}
$sHtml =preg_replace_callback ("/\[table (?: \ \ s*=\s* (\d{1,4}%?) \s* (?:, \s* ([^\]]+) \s*)?] /i ", ' getTable ', $sHtml);
function Gettr ($match) {return ' ';}
$sHtml =preg_replace_callback ("/\[tr (?: \ \ S*= (\s*[^\]]+)) \]/i ", ' gettr ', $sHtml);
function Gettd ($match) {
$col = $match [1]; $row = $match [2]; $w = $match [3];
Return ' 1. ' colspan= '. $col. ' "': '). ($row >1? ' rowspan= '. $row. ' "': '). ($w? ' width= '. $w. ' "': '). ' > ';
}
$sHtml =preg_replace_callback ("/\[td (?: \ \ s*=\s* (\d{1,2}) \s*,\s* (\d{1,2}) \s* (?:, \s* (\d{1,4}%?))?)? \]/i ", ' Gettd ', $sHtml);
$sHtml =preg_replace ("/\[\/(TABLE|TR|TD) \]/i", ' ', $sHtml);
$sHtml =preg_replace ("/\[\*\" ([^\[]+)/I ", '
  • $
  • ', $sHtml);
    function Getul ($match)
    {
    $str = '
    if ($match [1]) $str. = ' type= '. $match [1]. ' "';
    return $str. ' > ';
    }
    $sHtml =preg_replace_callback ("/\[list (?: \ \ s*=\s* ([^\]]+) \s*) \]/i ", ' Getul ', $sHtml);
    $sHtml =preg_replace ("/\[\/list\]/i", '', $sHtml);
    return $sHtml;
    }
    ?>

    http://www.bkjia.com/PHPjc/364726.html www.bkjia.com true http://www.bkjia.com/PHPjc/364726.html techarticle 16 Fire 84 Network (Liehuo. net) teach 17 Cheng recently to download column add a key reprint function, fire download column is the use of dedecms built, we all know Dede not a key reprint function, ...

  • 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.