...

View Full Version : highlight_string(); by user input



tribalmaniac
01-26-2007, 05:11 PM
I'm creating a new forum.
Similar to this forum, users will be able to use the "php" tags, I already have code that converts BBCODE to HTML, but I was just wondering how I would go about including a highlight_string(); php function into it?

my code is currently this:


function output_post ($post) {
$post_no_html = htmlspecialchars($post);
$post_abridged = chop($post_no_html);
$bbcode_regex = array(0 => '/\[b\](.+?)\[\/b\]/s',
1 => '/\[i\](.+?)\[\/i\]/s',
2 => '/\[u\](.+?)\[\/u\]/s',
3 => '/\[url\](.+?)\[\/url\]/s',
4 => '/\[url\=(.+?)\](.+?)\[\/url\]/s',
5 => '/\[img\](.+?)\[\/img\]/s',
6 => '/\[color\=(.+?)\](.+?)\[\/color\]/s',
7 => '/\[br\]/s',
8 => '/\[hr\]/s',
9 => '/\[center\](.+?)\[\/center\]/s',
10 => '/\[size\=(.+?)\](.+?)\[\/size\]/s');

$bbcode_replace = array(0 => '<b>$1</b>',
1 => '<i>$1</i>',
2 => '<u>$1</u>',
3 => '<a href="$1">$1</a>',
4 => '<a href="$1">$2</a>',
5 => '<img src="$1" alt="User submitted image" title="User submitted image"/>',
6 => '<span style="color:$1">$2</span>',
7 => '<br />',
8 => '<hr />',
9 => '<center>$1</center>',
10 => '<span style="font-size:$1">$2</span>');

ksort($bbcode_regex);
ksort($bbcode_replace);

//preg_replace to convert all remaining bbCode tags
//$post_bbcode_treated = preg_replace($bbcode_regex, $bbcode_replace, $post_abridged);

$output = preg_replace($bbcode_regex, $bbcode_replace, $post_abridged);

$open = '<table class="quote"><tr><td>';
$close = '</td></tr></table>';

// How often is the open tag?
preg_match_all ('/\
/i', $output, $matches);
$opentags = count($matches['0']);

// How often is the close tag?
preg_match_all ('/\[\/quote\]/i', $output, $matches);
$closetags = count($matches['0']);

// Check how many tags have been unclosed
// And add the unclosing tag at the end of the message
$unclosed = $opentags - $closetags;
for ($i = 0; $i < $unclosed; $i++) {
$output .= '</td></tr></table>';
}

// Do replacement
$output = str_replace ('[quote]', $open, $output);
$output = str_replace ('', $close, $output);

return $output;

}

tribalmaniac
01-27-2007, 10:26 PM
Anyone have any ideas?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum