Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[PHP BBcode] Converting BBcode to HTML

Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Today I'll go over converting BBcode, (like [b ], [/b]), into HTML. Also, the reasons why people do this, and how to honor those reasons in your applications.

Why BBcode?
Security from XSS Injections, yet a way for user-submitted content to be a little more creative & stylized. Letting the user have full access to putting HTML on your site is a huge security risk. The user can put things like iFrames, JavaScript, or other scripts that can possibly hijack your site. To avoid that in a simple way that most people can identify, is to use BBcode.

Getting Started
So first, you need to make a form with a textarea that can be submitted. Or you can acquire a code editor like MarkItUp (google it), which by default takes HTML code, but can be optimized to use BBcode. They have a BBcode->HTML converter, and some buttons that can be downloaded and integrated, but you have to do a bit more to make your editor have all the buttons and features I'll be using. I optimized mine to have a PHP button, for PHP code. It also highlights the syntax, and displays the code with numbered lines when it displays. I do that because I'm going to be using it on my site, and developers need a way to submit code in a secure environment. I'll be contributing to MarkItUp, so they might later release my scripts, if they like them. Who knows :cool:

To start, I'm not going to show you how to make a form or an action or any of that, I'm just going to show you how to convert your BBcode into HTML. The way you decide to implement this is your choice.
The First BBCode
Okay, so basically, we typically use REGEX to do this. It makes it easy to do it in PHP, and is pretty flexible. Allot of developers have a problem with that, but as long as you use the converter in a good way (like parsing it before putting it in the database, rather than parsing it every time it's loaded), it's really not that bad on the server. Logically, it's an easy way to take user-generated HTML minus the XSS possibilities. It's very important to assume the user will submit malicious HTML, so the main thing we've got to do is use a function to disable that HTML. I use htmlentities() in this example, since I want users to put HTML code that will be seen as HTML code, but I don't want it to work.

So to make it work, we mostly use the PHP function,
PHP:
preg_replace($search, $replace, $string);
The search/replace arguments are special for this function. Like other similar functions, they can be arrays or strings. What makes them special, is the REGEX. We can code patterns into the search statement, and even have the pattern return something special to the 'replace' argument. So we can convert [ b]text[/b] to <b>text</b>, which in return, will output: text.

Take this example,
PHP:
$text = '[b]text[/b].';
echo preg_replace('/\[b\](.*)\[/b\]/si','<strong>\1</strong>',$text);
The text will be returned bold. Using this basic syntax, we can do allot of things. I'll steal the idea of everyone else by adding some more passable code than just bold, below. If you've never used REGEX, or don't know what it is, that might seem a little weird-looking to you. Questions like, "why is there so many slashes?", or "Huh? But how does it know what's between the brackets?" Those questions are quite simply answered. The backslashes are there because they are used to escape certain characters that get seen as REGEX. We call these, "Special Characters." The forward slashes are put on the outside of the REGEX pattern, (and can also be # signs). They are used to tell preg_ that it needs to look for a given pattern. Without the declaration of a pattern, there is no pattern. Also, on notice the 'si' after the pattern. The 's' and the 'i' are called modifiers. They give special action to the pattern they follow. Here's some documentation on Modifiers, so you can get a little more familiar,


Basically, the 's' lets the variable inside (.*) include multiple lines of text, and the 'i' let's the BBcode ignore case-sensitivity. (so [b ][/b] is the same as [B ][/B], and [b ][/B], etc.)

(Edited)
Most importantly, notice the parenthesis between the BBcode in the pattern (.*). The "period" (.) (meaning any type of character; {with the 's' modifier, line-breaks are considered characters.} ), followed by the "asterisk" (*) (meaning all) signals that any amount of any character might come up there. That includes the option of 0 characters. (.+) with the 'Usi' modifiers will mean any amount greater than 0..
Really Building The Script Now,
Next, let's start making a function that does all the BBcode at once, (Also, a bit more BBcode):
PHP:
BBcodeToHTML($text)
{
    $text = trim(htmlentities($text));
    // BBCode to find...
    $in = array( 
                     '/\[b\](.*?)\[\/b\]/si',    
                     '/\[i\](.*?)\[\/i\]/si',
                     '/\[u\](.*?)\[\/u\]/si',
                     '#\[img\](https?://[-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|]\.(?:jpg|jpeg|gif|png|bmp))\[\/img\]#si',
                     '#\[email\]([-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|])\[\/email\]#si',
                     '#\[url\=((?:ftp|https?)://[-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|])\](.*?)\[\/url\]#si',
                     '/\[size\="?(.*?)"?\](.*?)\[\/size\]/si',
                     '/\[color\="?(.*?)"?\](.*?)\[\/color\]/si',
                     '/\[list\=(.*?)\](.*?)\[\/list\]/si',
                     '/\[list\](.*?)\[\/list\]/si',
                     '/\[\*\]\s?(.*?)\n/si'
    );
    // And replace them by...
    $out = array(
                     '<strong>\1</strong>',
                     '<em>\1</em>',
                     '<u>\1</u>',
                     '<img src="\1" alt="\1" />',
                     '<a href="mailto:\1">\1</a>',
                     '<a href="\1">\2</a>',
                     '<span style="font-size:\1%">\2</span>',
                     '<span style="color:\1">\2</span>',
                     '<ol start="\1">\2</ol>',
                     '<ul>\1</ul>',
                     '<li>\1</li>'
    );
    return preg_replace($in, $out, $text);
}
There's a bit more in this function, but it does the same basic thing, just over and over with different possibilities. Also, there's some examples of multiple arguments, such as a link, and color BBcode.

The Dreaded [ QUOTE ] Recursive,
For certain BBcode Tags, it's not as simple as simply putting them in an array like that. For certain cases, we need to use the preg_replace_callback() function, which "calls back" to itself, looking to make a recursive tag.(Such as a quote, inside a quote, inside a quote, etc, in a hierarchy)
As you can see, RageZone has one of these, and I discovered how to do this (with the help of documentation, but it took a bit of other knowledge too; Google didn't help me much either...). (Hey, I'm proud of myself, okay, REGEX isn't my expertise).

Anyway, I modified the "parser" (I call it a "converter"), from MarkItUp, and it's way better than it was when I got it.

So we use a function like this,
PHP:
// BBCode [ quote ] / [ quote="name" ]
function parseQuotesRecursive($input)
{
    $regex = '#\[quote\=?"?(.*?)"?\]((?:[^[]|\[(?!/?quote\=?"?(.*?)"?\])|(?R))+)\[/quote\]#i';
    if (is_array($input)) 
    {
        $input = '<blockquote style="margin: 5px 20px 20px;">'
            .'<div style="margin-bottom: 2px;font-size:10px;font-family:sans-serif">'
            .'Quote'.( (strlen($input[1])>0) ? ' From <strong>'.$input[1].'</strong>' : '' ).','
            .'</div>'
            .'<div style="padding:4px;padding-top:0;border:#000 1px inset">'
            .$input[2]
            .'</div>'
            .'</blockquote>';
    }
    return preg_replace_callback($regex, 'parseQuotesRecursive', $input);
}
$text = parseQuotesRecursive($text);
The REGEX is very complicated to explain, the key parts of it are in the middle, and are shown in the last example on the preg_replace_callback() function reference,


The key part that makes the function I made unique from the PHP example, is this:
Code:
\=?"?(.*?)"?
The question marks means 'maybe', as I explained above. So that way the equal sign, the quotes, and the value between them is all just a big 'maybe'. If a user forgets to close a quote, or even forgets an equal sign, it should still show the name, if it's entered. They do need to enclose the brackets. That just allows a 'name' to be used in the quote. So it can say, 'Quote From Spence,' or whoever it's from, optionally.

Additionally, one can choose not to include a name, if they don't know who a quote is from, or for whatever reason...

This part (in the center) makes it recursive. It's unique to the quote function, in my case.
Code:
((?:[^[]|\[(?!/?quote\=?"?(.*?)"?\])|(?R))+)
That whole snippet is a big maybe, and it is recursive. That means, you can have an infinite amount of quotes inside other quotes, and an infinite number of combinations, so long as all the quotes are properly closed. The whole thing is very flexible.
Now moving on to the [ code ] and [ php ] tags,
I'll be doing those a little different, too.

To start, I got a script and modified it from a comment at PHP.net, under highlight_string() function reference. That function highlights PHP code with the built-in syntax highlighter, and puts them in tags so we can see the PHP code. It's actually a really cool function, and makes my life very easy in that respect.

So here's the function I use for [ code ] and [ php ] BBcode tags, (You may have to modify it yourself, that's why I told you where I got it from.)
PHP:
<style type="text/css">
/* Syntax Highlight Code Box */
.linenum{
    text-align:right;
    background:#FDECE1;
    border:1px solid #cc6666;
    padding:18px 1px 18px 1px;
    font-family:Courier New, Courier;
    float:left;
    width:17px;
    margin:3px 0px 30px 0px;
    }

code    {/* safari/konq hack */
    font-family:Courier New, Courier;
    margin:0;
    padding:0;
}

.linetext{
    width:700px;
    text-align:left;
    background:white;
    border:1px solid #cc6666;
    border-left:0px;
    padding:0px 1px 0px 8px;
    font-family:Courier New, Courier;
    float:left;
    margin:3px 0px 30px 0px;
    }

br.clear    {
    clear:both;
}
</style>
<?php
function printCode($code, $lines_number = 0,$title='Code: ')
{
    $break = '
';
    if (!is_array($code)) $codeE = explode($break, $code);
    $count_lines = count($codeE);
    
    $r = '<div style="margin-bottom: 2px;font-size:10px;font-family:sans-serif">'
        .$title
        .'</div>';
    
    if ($lines_number)
    {           
        $r .= '<div class="linenum">';
        for($i=1;$i<=$count_lines;$i++)
        {   
            $r .= $i.'<br />';
        }
        $r .= '</div>';
    }

    $r .= '<div class="linetext">';
    $r .= highlight_string($code,1);
    $r .= (strpos($code,'<?php')===false?$break:'');
    $r .= '</div>';

    return '<div class="code">'.$r.'</div>';
}
?>
The first argument takes the code you want to manipulate. The second argument is a boolean (true/false), that will enable line numbers (or not). The last argument is the title of your code box.. Like PHP:, or CODE:, or whatever. It's a pretty easy function to use.

That function alone won't read through the BBcode, it's just to help us with these function that do:
PHP:
<?php
// BBCode [ PHP ] [By Spencer A. Lockhart]
function php_box($s) 
{
    $code = stripslashes($s[1]);
    $break = '
';
    if(strpos($code,'<?php')===false) $code = '<?php '.$break.$code.$break.'?>';
    return printCode( html_entity_decode($code), true, 'PHP: ');
}
$text = preg_replace_callback('/\[php\](.*?)\[\/php\]/Usi','php_box',$text);

// BBCode [ code ]
function escape($s) 
{
    return printCode( html_entity_decode($s[1]), true);
}    
$text = preg_replace_callback('/\[code\](.*?)\[\/code\]/msi', "escape", $text);
?>
I put a patch on the PHP one. The syntax highlighter doesn't seem to work without PHP tags, so I just add them in if they don't exist. Otherwise, the user can mix PHP/other code inside the PHP tags, and only the PHP code will be highlighted. It's actually pretty flexible, too. (Just as the Code tags, they're pretty much the same, minus the patch).
Okay, Skip to the Whole Script,
html_entity_decode() is only needed if you use htmlentities() before these functions are called.

Altogether, your "parser" (as some would call it; or more correctly, your "BBcode->HTML converter"), should look something like this: (give/take some extra added things)
PHP:
<style type="text/css">
/* Syntax Highlight Code Box */
.linenum{
    text-align:right;
    background:#FDECE1;
    border:1px solid #cc6666;
    padding:18px 1px 18px 1px;
    font-family:Courier New, Courier;
    float:left;
    width:17px;
    margin:3px 0px 30px 0px;
    }

code    {/* safari/konq hack */
    font-family:Courier New, Courier;
    margin:0;
    padding:0;
}

.linetext{
    width:700px;
    text-align:left;
    background:white;
    border:1px solid #cc6666;
    border-left:0px;
    padding:0px 1px 0px 8px;
    font-family:Courier New, Courier;
    float:left;
    margin:3px 0px 30px 0px;
    }

br.clear    {
    clear:both;
}
</style>
<?php
function printCode($code, $lines_number = 0,$title='Code: ')
{
    $break = '
';
    if (!is_array($code)) $codeE = explode($break, $code);
    $count_lines = count($codeE);
    
    $r = '<div style="margin-bottom: 2px;font-size:10px;font-family:sans-serif">'
        .$title
        .'</div>';
    
    if ($lines_number)
    {           
        $r .= '<div class="linenum">';
        for($i=1;$i<=$count_lines;$i++)
        {   
            $r .= $i.'<br />';
        }
        $r .= '</div>';
    }

    $r .= '<div class="linetext">';
    $r .= highlight_string($code,1);
    $r .= (strpos($code,'<?php')===false?$break:'');
    $r .= '</div>';

    return '<div class="code">'.$r.'</div>';
}

function BBCodeToHTML($text) {
    $text = trim(htmlentities($text));
    
    // BBCode [ PHP ] [By Spencer A. Lockhart]
    if(!function_exists('php_box'))
    {
        function php_box($s) 
        {
            $code = stripslashes($s[1]);
            $break = '
';
            if(strpos($code,'<?php')===false) $code = '<?php '.$break.$code.$break.'?>';
            return printCode( html_entity_decode($code), true, 'PHP: ');
        }
    }
    $text = preg_replace_callback('/\[php\](.*?)\[\/php\]/Usi','php_box',$text);
    
    // BBCode [ code ]
    if (!function_exists('escape')) {
        function escape($s) {
            return printCode( html_entity_decode($s[1]), true);
        }    
    }
    $text = preg_replace_callback('/\[code\](.*?)\[\/code\]/msi', "escape", $text);
    
    // BBCode [ quote ] / [ quote="name" ]
    if(!function_exists(parseQuotesRecursive))
    {
        function parseQuotesRecursive($input)
        {
            $regex = '#\[quote\=?"?(.*?)"?\]((?:[^[]|\[(?!/?quote\=?"?(.*?)"?\])|(?R))+)\[/quote\]#i';
            if (is_array($input)) 
            {
                $input = '<blockquote style="margin: 5px 20px 20px;">'
                    .'<div style="margin-bottom: 2px;font-size:10px;font-family:sans-serif">'
                    .'Quote'.( (strlen($input[1])>0) ? ' From <strong>'.$input[1].'</strong>' : '' ).','
                    .'</div>'
                    .'<div style="padding:4px;padding-top:0;border:#000 1px inset">'
                    .$input[2]
                    .'</div>'
                    .'</blockquote>';
            }
            return preg_replace_callback($regex, 'parseQuotesRecursive', $input);
        }
    }
    $text = parseQuotesRecursive($text);

    //BBcode to Find..
        $in = array( 
                     '/\[b\](.*?)\[\/b\]/si',    
                     '/\[i\](.*?)\[\/i\]/si',
                     '/\[u\](.*?)\[\/u\]/si',
                     '#\[img\](https?://[-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|]\.(?:jpg|jpeg|gif|png|bmp))\[\/img\]#si',
                     '#\[email\]([-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|])\[\/email\]#si',
                     '#\[url\=((?:ftp|https?)://[-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|])\](.*?)\[\/url\]#si',
                     '/\[size\="?(.*?)"?\](.*?)\[\/size\]/si',
                     '/\[color\="?(.*?)"?\](.*?)\[\/color\]/si',
                     '/\[list\=(.*?)\](.*?)\[\/list\]/si',
                     '/\[list\](.*?)\[\/list\]/si',
                     '/\[\*\]\s?(.*?)\n/si'
    );
    // And replace them by...
    $out = array(
                     '<strong>\1</strong>',
                     '<em>\1</em>',
                     '<u>\1</u>',
                     '<img src="\1" alt="\1" />',
                     '<a href="mailto:\1">\1</a>',
                     '<a href="\1">\2</a>',
                     '<span style="font-size:\1%">\2</span>',
                     '<span style="color:\1">\2</span>',
                     '<ol start="\1">\2</ol>',
                     '<ul>\1</ul>',
                     '<li>\1</li>'
    );
    $text = preg_replace($in, $out, $text);
        
    // paragraphs
    $text = str_replace("\r", "", $text);
    $text = "<p>".ereg_replace("(\n){2,}", "</p><p>", $text)."</p>";
    $text = nl2br($text);
    
    // clean some tags to remain strict
    // not very elegant, but it works. No time to do better ;)
    if (!function_exists('removeBr')) {
        function removeBr($s) {
            return str_replace("<br />", "", $s[0]);
        }
    }    
    $text = preg_replace_callback('/<pre>(.*?)<\/pre>/ms', "removeBr", $text);
    $text = preg_replace('/<p><pre>(.*?)<\/pre><\/p>/ms', "<pre>\\1</pre>", $text);
    
    $text = preg_replace_callback('/<ul>(.*?)<\/ul>/ms', "removeBr", $text);
    $text = preg_replace('/<p><ul>(.*?)<\/ul><\/p>/ms', "<ul>\\1</ul>", $text);
    
    return $text;
}
echo BBCodeToHTML(urldecode(stripslashes($_POST['data'])));
?>
You may have to take out some instances of stripslashes() if you don't have 'magic_quotes' enabled, or if you're entering it directly into a MySQL database. In which case, you should use mysql_real_escape_string(), as always. Protecting from XSS injections DO NOT protect from MySQL injections, they're completely different kinds of injections, and are both very bad in very different ways, and may worsen in different circumstances.

Mind you, I didn't write all this code, I just heavily improved it, and changed some of it around. If you want to see the original I had to work with, search MarkItUp on Google, and download their editor along with the BBcode add-ons etc.

I did things like the case-insensitive patch, protected from XSS injections, the code editor (I partially improved the function and the CSS for my liking, and number match-up unique to this script), the recursive quotes, the name for the quote, the PHP button & BBcode conversion, and an improvement on the [code ] functionality. Also, the mark-up for quotes, code, and PHP.
Credits: MarkItUp, LifeTaker

Hope you liked this tutorial :cool:
Good Luck! :thumbup:
 
Last edited:
Initiate Mage
Joined
Nov 2, 2009
Messages
54
Reaction score
21
. Anytime you use (.*) it means that something is expected in place of the '.*'. Something HAS TO BE in between the brackets for the REGEX to find a pattern there.

Wrong :D

. means any character, enabling the s option makes it catch new lines too.
* means ANY amount of characters. 0 or more. So it doesn't have to be there.

#\[b\](.+)\[/b\]#isU

That would've made it so that there has to be something in.

Other than that, nice guide ;D
 
Last edited:
Infraction Baɴɴed
Member
Joined
Apr 9, 2008
Messages
1,416
Reaction score
169
for the quote part, can you help with the linkback part.

quote=name;link
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
for the quote part, can you help with the linkback part.

quote=name;link

It's this part that does the name,
\=?"?(.*?)"?

So you can just change it to this,
\=?"?(.*?)"?(;"?(?:ftp|https?)://.*?"?)?

Edit: XSS Injection can still get through this and the [ URL] tag, see this post for more details!
http://forum.ragezone.com/5417679-post9.html

A fix will be issued as soon as I find one. Please look for more possible injections and let me know, I'll appreciate it allot ;) Remember, not all sources are secure like they say they are! They might not know :eek:, no matter who they are. Even Firefox has security issues sometimes, (they also admit there might be some unknown ones).

So please note, I do admit there might be some security issues that I don't know about, but still, the purpose of doing this is to decrease, or if possible, prevent XSS injections.

[Edit, Security Issue Fixed] Now it only allows certain special characters ;) Meaning, I can't use a quote or anything to make a link an XSS injection. So I think it's secure now.
PHP:
function parseQuotesRecursive($input)
	{
		/* // Piece-By-Piece:
			$url_pattern = '(?:ftp|https?)://[-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|]';
			$parm_pattern = '\=?"?(.*?)"?(;'.$url_pattern.')?';
			$regex = '#\[quote'.$parm_pattern.'\]((?:[^[]|\[(?!/?quote'.$parm_pattern.'\])|(?R))+)\[/quote\]#i';
		*/
		$regex = '#\[quote\=?"?(.*?)"?(;(?:ftp|https?)://[-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|])?\]((?:[^[]|\[(?!/?quote\=?"?(.*?)"?(;(?:ftp|https?)://[-A-Z0-9+&@\#/%?=~_|!:,.;]*[-A-Z0-9+&@\#/%=~_|])?\])|(?R))+)\[/quote\]#i';
		if (is_array($input)) 
		{
			$input = '<blockquote style="margin: 5px 20px 20px;">'
				.'<div style="margin-bottom: 2px;font-size:10px;font-family:sans-serif">'
				.'Quote'.( (strlen($input[1])>0) ? ' From <strong>'.$input[1].'</strong>' : '' )
				.( (strlen($input[2])>0) ?' (<a href="'.substr($input[2],1,strlen($input[2])).'" target="_blank">View</a>)':'').','
				.'</div>'
				.'<div style="padding:4px;padding-top:0;border:#000 1px inset">'
				.$input[3]
				.'</div>'
				.'</blockquote>';
		}
		return preg_replace_callback($regex, 'parseQuotesRecursive', $input);
	}
	$text = parseQuotesRecursive($text);

Note, also updating the issues in the tutorial ;)

Now you can use [ quote], [ quote=name], [ quote;url], and [ quote=name;url]; Somehow, I managed to do this. Thanks to you, I actually fried my brain today.. Also thanks to you, I found and fixed a security hole! Thanks! lol.. Never again will I spend so much time on REGEX.. Never again.. *Hopes I don't eat my words*
 
Last edited:
Infraction Baɴɴed
Member
Joined
Apr 9, 2008
Messages
1,416
Reaction score
169
EDIT:
going off what i sent you in the pm i managed to get it to the way i wanted (somewhat)

PHP:
function parseQuotesRecursive($input) {
	$etc_pattern = '\=?"?(.*?)"?(;"?.*?"?)?';
	$regex = '#\[quote'.$etc_pattern.'\]((?:[^[]|\[(?!/?quote'.$etc_pattern.'\])|(?R))+)\[/quote\]#i';
	if (is_array($input)) {
		$input = '<blockquote style="margin: 5px 20px 20px;">'
			.'<div style="margin-bottom: 2px;font-size:10px;font-family:sans-serif">'
			.'Quote'.( (strlen($input[1])>0) ? ' From <strong>'.$input[1].'</strong>' : '' )
			.( (strlen($input[2])>0) ?' (<a href="'. (int) substr($input[2],1,strlen($input[2])).'" target="_blank">View</a>)':'').','
			.'</div>'
			.'<div style="padding:4px;padding-top:0;border:#000 1px inset">'
			.$input[3]
			.'</div>'
			.'</blockquote>';
	}
	return preg_replace_callback($regex,'parseQuotesRecursive', $input);
}
 
Last edited:
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Oh nice, it's still secure now since it won't take anything except integers..

You might also try doing something like this,
PHP:
'\=?"?(.*?)"?(;[-\#0-9a-z]*[-\#0-9a-z]?)?';
I noticed ragezone uses all those characters.

The link needs an anchor (at least), doesn't it? Anyway, you can do this and eliminate the need for an (int). I removed the quotes because they can cause issues. You really don't need them anyway.. You want to do all the filtering directly through the REGEX. So for example, instead of using (int), you should do '[0-9]*[0-9]' in REGEX. I just assumed you really needed more than an int ;)

Nice though ;)
 
Infraction Baɴɴed
Member
Joined
Apr 9, 2008
Messages
1,416
Reaction score
169
Oh nice, it's still secure now since it won't take anything except integers..

You might also try doing something like this,
PHP:
'\=?"?(.*?)"?(;[-\#0-9a-z]*[-\#0-9a-z]?)?';
I noticed ragezone uses all those characters.

The link needs an anchor (at least), doesn't it? Anyway, you can do this and eliminate the need for an (int). I removed the quotes because they can cause issues. You really don't need them anyway.. You want to do all the filtering directly through the REGEX. So for example, instead of using (int), you should do '[0-9]*[0-9]' in REGEX. I just assumed you really needed more than an int ;)

Nice though ;)
the link is going to showthread.php?threadid=*number*. that is why i wanted it to only get numbers.
 
Infraction Baɴɴed
Member
Joined
Apr 9, 2008
Messages
1,416
Reaction score
169
awe what the hell. no one is posting or helping in this thread so here are some misc codes.
YouTube:
Code:
$text = preg_replace("/\[youtube\](.+?)\[\/youtube\]/is", "<embed height=\"340px\" width=\"560px\" type=\"application/x-shockwave-flash\" src=\"http://www.youtube.com/v/\\1\"/>", $text); // [youtube]URL[/img]
ex: take youtube.com/v/zq87377usKY&NR and put it in [ youtube]zq87377usKY&NR[/youtube]



Progression bar:
Code:
$text = preg_replace("/\[progress\](.+?)\[\/progress\]/is", progress("\\1"), $text);
Function:
Code:
function progress($text) {
	$output = "<div style=\"background-color: red; width:100px; border: 1px solid black;\">";
	$output .= "<div style=\"background-color: green; color: white; font-weight:bold; max-width: 100px; width:".$text."px;\">   ".$text."%</div>";
	$output .= "</div>";
	return $output;
}

ex: just put in anything below 100 and it will show in a green bar.
 
Last edited:
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
With the Progress Bar, have you hooked it up to do anything functional? I need a JS progress bar that fills up as a PHP document is loaded. (I have a mass AJAX installer I'm working on)

Just wondered if you've done anything like that already that I can peek at to skip a few hours :)
 
Infraction Baɴɴed
Member
Joined
Apr 9, 2008
Messages
1,416
Reaction score
169
nah. i have no idea how ajax works lol. i only use jquery to update a div every 2 sec so, other then that sorry cant help.

EDIT:
i would say to make the script output a int to the progress function to auto update the bar. again, not sure how ajax functions so just throwing out what i think it does.

EDIT2:
thinking of vBulletin installer, you could assign a variable to output to update the bar?
 
Last edited:
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
Yeah.. I can rig it to work, just wanted an easy way out :p

I think I'll make it so a JS function draws the progress bar with a few easy numbers. The total amount of files divided by **** is the max. The amount of files loaded divide by 100 is the percent complete.. A little rounding here/there.. So when the files are extracting, it will try to do one at a time, and leave details as to where it left off. Since the installer will take ages to complete, in some cases, then it needs to save the progress, and be compatible to pick up where it left off.

There's a bit more to it, but that's the basics. I suppose I'll have allot of work to do, lol.. Not today though.. I'll talk more about this in some other post some other day..

Thanks anyway ;)
 
Experienced Elementalist
Joined
Apr 15, 2008
Messages
256
Reaction score
0
Thank you very much for this piece of code.
Greatly appreciate it
 
Back
Top