MyBB Hacks

Full Version: Variable MyCode?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is probably a pretty insane idea for a MyCode. Hell, I'm not sure if it was ever thought of before. If I'm not making much sense, I blame me being sleepy when I thought of it. I haven't fully thought everything out as I've just thought of it, but again, I hope it makes some sense.

The MyCode would let users define variables (like variables in various programming languages) and then another can refer to it. The syntax could look a little something like this:

Code:
[variable=([a-z0-9]+)]Information that could later be reused in the post.[/variable] <-- Defining a variable.
[variable=([a-z0-9]+)] <-- Referring to a variable.


Example:

Code:
[variable=lorem]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc id blandit elit. Nullam euismod urna at leo blandit eget pulvinar neque interdum.[/variable]

[variable=lorem]
[variable=lorem]
[variable=lorem]


I was thinking the variables could be restricted to the post it was defined in, that way other posts can't access them.

For ease with regular expressions you may wish to use different tags for setting and getting the variable.

Lazy example code (PHP 5.3 only cause I'm extra lazy):

PHP Code:
$postvars = array();
$post['message'] = preg_replace_callback('~\[set\=([a-z0-9]+)\](.*?)\[/set\]~si', function($m) use(&$postvars) {
	$postvars[strtolower($m[1])] = $m[2];
}, $post['message']);
$post['message'] = preg_replace('~\[variable=([a-z0-9]+)\]~ie', '$postvars[strtolower(\'$1\')]', $post['message']);

Reference URL's