Version: 2
Type: Function
Category: Other
License: GNU General Public License
Description: Version 1 went right to the bin, because of some stupid error’s i made :(. (people interested can email me :))
The UBB Parser requires a Tags.inc.php file, wich i’m not going to supply to you :P, ’cause it’s really easy to make 😉
There isn’t any support for tags like [url=http://google.com]Link to google[/url] and you’ll have to settle with [url=http://google.com]
All the standard tags are supported (like [b] (bold) and [i] italic), if you find any bugs don’t hasitate to email me.
Because the function arguments are called by referance you can’t put plain text in it, and you’ll have to do with variables.
array construction: (Tags.inc.php) <?php $Tags = array ( 'url' => array //The tag's name ( '<a ', //Opening (HTML) part '</a>',//Closing part 'href='*'>Link to: '//The argument, replace with false for no argument, * will be replaced by the given argument i.e [url=http://google.com] will take out http://google.com and replace * with it ) ); ?> UBB.php: <?php /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * UBB parser V2, Parses UBB (Ultimate Bulletin Board) tags * * Copyright (C), Jasper Bekkers (PrisonerOfPain) <[email protected]>, 2004 * * * * This program is free software; you can redistribute it and/ * * or modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation; either version 2 * * of the License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * General Public License for more details. * * * * You should have received a copy of the GNU General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place, Suite 330, * * Boston, * * MA 02111-1307 USA * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ error_reporting (E_ALL); include_Once ('Tags.inc.php');//Tag definitions here =) $OpenTag = '[';//IE ->>[b] $CloseTag = ']';//IE [b]<<- $Slash = '/';//IE [->>/b] $Equal = '=';//For the parameter [url->>=http://google.com] $Needle = '*';// define ('NEWLINE', chr(13)); function UBB (&$Input) { global $Tags, $OpenTag, $CloseTag, $Slash, $Equal, $Needle, $NestedTags, $NestedText; if (empty ($Input))//We dont want a empty string as $Input return false; $Input = explode (NEWLINE, $Input); if ($PossibleTag = preg_grep ('[' . $OpenTag . ']', preg_grep ('[' . $CloseTag .']', $Input)))//Definitly needs to be replaced {//Check if the string matches the opentag aswell as the close tag foreach ($PossibleTag as $Key => $Value)//Walk trough every possible tag to identify one { foreach ($Tags as $tTag => $Content)//walk trough all tags { $ToReplaceOpen = $OpenTag . $tTag . $CloseTag; $ToReplaceClose = $OpenTag . $Slash . $tTag . $CloseTag; if (subStr_Count ($Value, $ToReplaceOpen )) {//We've got a open tag, IE [b] or [i] if (!empty ($Content[1]))//The tag has a corresponding closing tag { if (isSet ($OpenedTags[$tTag]))//Tag allready opened { $OpenedTags[$tTag]['amount']++; $OpenedTags[$tTag]['position'] = $Key; } else { $OpenedTags[$tTag]['amount'] = 1; $OpenedTags[$tTag]['position'] = $Key; } if (subStr_Count ($Value, $ToReplaceClose)) { if (subStr_Count ($Value, $ToReplaceClose) != $OpenedTags[$tTag]['amount']) { $Input[$OpenedTags[$tTag]['position']] = str_Replace ($ToReplaceOpen,$Content[0],$Input[$OpenedTags[$tTag]['position']]);//Replace open tag $Input[$Key] = str_Replace ($ToReplaceClose, $Content[1], $Input[$Key]);//replace close tag } else { $Input[$OpenedTags[$tTag]['position']] = str_ReplaceOnce ($ToReplaceOpen,$Content[0],$Input[$OpenedTags[$tTag]['position']]);//Replace open tag $Input[$Key] = str_ReplaceOnce ($ToReplaceClose, $Content[1], $Input[$Key]);//replace close tag } $OpenedTags[$tTag]['amount']--;//decrease amount of open tags $OpenedTags[$tTag]['position'] = -1;//remove position } }//close open/ open-close tag else {//Found open tag that doesn't need closing! $Input[$Key] = str_ReplaceOnce ($ToReplaceOpen , $Content[0], $Input[$Key]); }//close singlesided tag }//close open tag elseif (isSet ($OpenedTags[$tTag]) && subStr_Count ($Value, $ToReplaceClose) && $OpenedTags[$tTag]['amount'] > 0 && $OpenedTags[$tTag]['position'] != -1) {//We've got a closing tag, ie [/b] or [/i] $Input[$OpenedTags[$tTag]['position']] = str_ReplaceOnce ($ToReplaceOpen,$Content[0],$Input[$OpenedTags[$tTag]['position']]);//Replace open tag $Input[$Key] = str_ReplaceOnce ($ToReplaceClose, $Content[1], $Input[$Key]);//replace close tag $OpenedTags[$tTag]['amount']--;//decrease amount of open tags $OpenedTags[$tTag]['position'] = -1;//remove position }//close close tag elseif (subStr_Count ($Value, $OpenTag . $tTag . $Equal) >= 1)//Possible arguments in tag { $temp = explode ($OpenTag . $tTag . $Equal,$Value); if (strPos ($temp[1], $CloseTag)) { $StrPos = strPos ($temp[1], $CloseTag); $Right = subStr ($temp[1], $StrPos); $Left = subStr ($temp[1], 0, $StrPos); $Left = $temp[0] . $Content[0] . str_ReplaceOnce ($Needle, $Left, $Content[2]) . $Left . $Content[1]; $Right = subStr ($Right, 1); $Input[$Key] = $Left . $Right; $Value = $Left . $Right; if (!empty ($Right)) reset ($Tags);//Critical here! must not be touched! else a whole lot of bad things run in to you at once :P unset ($temp);//Clean up.... } }//Close 'tag has argument' }//close foreach (Tags) loop }//close foreach loop }//close possible tag return ($Input = implode (' ', $Input)); }// close function function str_ReplaceOnce ($search, $replace, $subject) { $StrPos = StrPos ($subject, $search); $StrLen = StrLen ($search); return subStr ($subject, 0, $StrPos) . $replace . subStr ($subject, $StrPos + $StrLen); } ?> Call: <?php echo UBB ($TextToParse); //This is allowed allso :) UBB ($TextToParse); echo $TextToParse; ?>