Jump to content

Need Help w/ a function needed to be done inside a preg_replace()


sw0o0sh

Recommended Posts

Thanks for reading to anybody concerned;

 

Anyway, I've been working on coding my own forum, it's pretty much done, a mimic of proboards, but anyway, the BBcode is kind of screwy. I never could think of else they would do bbcode without preg_replace, as I've never really looked into another forums source if it was open, but anyway, I have an issue when it comes to quoting users.

 

Say somebody goes to quote a topic, it'll open up the post form with something like in it by default..

 

Bla Bla

 

The preg_replace manages like this,

 

$bb = preg_replace("/\[quote user\=(.+?)\]/i", "Quoting: $1 <table align='center' border='1' bordercolor='black' class='itable'><tr><td valign='top'>", $bb);
$bb = preg_replace("/\[\/quote\]/i", "</td></tr></table>", $bb);

 

Anyway, fault since I don't know what to do if theres line breaks (say the user doesn't add [/ quote]), but that's not even my main issue.

 

I need the username to run through a few functions. All usernames that are displayed on a page run through a function to show their "display name" and not their "account name". And due to an old version of the forum, even the profile needs to run through a function to return their user ID.

 

this is what i would have preferred...

 

$bb = preg_replace("/\[quote user\=(.+?)\]/i", "Quoting: <a href='?act=profile&id=" . id($1) . "'>". return_dn($1) ."</a> <table align='center' border='1' bordercolor='black' class='itable'><tr><td valign='top'>", $bb);
$bb = preg_replace("/\[\/quote\]/i", "</td></tr></table>", $bb);

 

But when I try that (notice the link in the above example), it returns this error..

 

Parse error: parse error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in C:\Documents and Settings\Tim\Desktop\website\cw_new\inc\bbc.php on line 24

 

For a half attempt at fixing it, i tried quoting the variables instead..

 

<a href='?act=profile&id=" . id("$1") . "'>". return_dn("$1") ."</a> 

 

However, now, the variables pass through the function however nothing is returned...

 

Any help is well appreciated.. I'm stumped >.>

$bb = preg_replace("/\

/ie", "functionToRun('$1')", $bb);

 

Notice the "e" modifier.

 

Here is a BBCode class that I made.  It's not the most efficient way to do it, seeing as I haven't quite cleaned it up yet, but it works pretty well.

 

<?php
class bbcode {

/**
 * List of supported tags
 *
 */
var $_tagList = array(
	'b'		=> '<b>{TEXT}</b>',
	'u'		=> '<u>{TEXT}</u>',
	'i'		=> '<i>{TEXT}</i>',
	'color'	=> '<span style="color: {OPT}">{TEXT}</span>'
);

/**
 * List of tags with an optional attribute
 *
 */
var $_attrList = array(
	'color'	=> "((#[0-9A-F]{3,6})|([A-Z\-]+))"
);
var $noBB = array();
var $bbID = 0;

/**
 * Convert from a standard input to HTML
 *
 * @param string $input
 * @return string
 */
function toHTML($input) {

	// Loop through the taglist and convert all
	foreach ( $this->_tagList as $tagKey=>$tagHTML ) {
		$tagOpen = "[{$tagKey}]";
		$tagClose = "[/{$tagKey}]";

		// Find the first instance of the current tag item
		if (!$this->_attrList[$tagKey]) {
			while ((stripos($input, $tagOpen) !== FALSE) && (strripos($input, $tagClose) !== FALSE)) {
				$position = stripos($input, $tagOpen);
				if ($position === FALSE) continue;
				$endposition = strripos($input, $tagClose, $position);
				if ($endposition === FALSE) continue;

				// Replace the text
				$newText = substr($input, $position + strlen($tagOpen), $endposition - ($position + strlen($tagOpen)));
				$newText = str_replace("{TEXT}", $newText, $tagHTML);
				$input = substr($input, 0, $position) . $newText . substr($input, $endposition + strlen($tagClose));
			}
		} else {
			$tagOpen = "@\[{$tagKey}=".$this->_attrList[$tagKey]."\]@is";
			while (preg_match($tagOpen, $input) && (strripos($input, $tagClose) !== FALSE)) {
				preg_match($tagOpen, $input, $match);
				$position = stripos($input, $match[0]);
				if ($position === FALSE) continue;
				$endposition = strripos($input, $tagClose, $position);
				if ($endposition === FALSE) continue;

				// Replace the text
				$newText = substr($input, $position + strlen($match[0]), $endposition - ($position + strlen($match[0])));
				$tagHTML = str_replace("{OPT}", $match[1], $tagHTML);
				$newText = str_replace("{TEXT}", $newText, $tagHTML);
				$input = substr($input, 0, $position) . $newText . substr($input, $endposition + strlen($tagClose));
			}
		}
	}
	return $input;
}
}

  • 1 month later...

Thanx.  Didn't know about the e modifier...

 

but I can't seem to get the e modifier working.

 

My quote preg_replace =

 

$quote = preg_replace(
	'/\[quote\=(.+?) date\=(.+?)\](.+?)\[\/quote\]/ie',
	"<div class=\"quote_header\">$1 " . time_format('$2') . "</div><div class=\"quote_body\">$3</div>",
	$quote
	);

 

most likely an escaping issue, but I've been trial & error'ing for a while, to no avail.

 

Advice?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.