Forum Moderators: coopster
I have been working on a system to add news and articles to a site, I have the core adding and removing out of the way and have had some very good advice from eelixduppy, the code for what i currently have is at the end of this post.
What i would to acheive is something where if i typed:
HELLO and wanted to make it bold i could wrap HELLO in something like the BBCODE on this forum. for example square brackets with a B inside to start the Bold area and square brackets with a /B inside to close the bold area, I would like to know how i can underline things with this too and perhaps add things such as font colours etc. a basic intro into how to get it to work would be great, here is the code i currently have if it helps in anyway at all.
<?php
include 'dbconnect.php';
$title = $_POST['title'];
$news = $_POST['news'];
$pattern = "/(http:\/\/[\w\.]+)/";
$replace = "<a href='$1'>$1</a>";
$title = preg_replace($pattern, $replace, $title);
$news = preg_replace($pattern, $replace, $news);
$title = mysql_real_escape_string($title);
$news = mysql_real_escape_string(nl2br($news));
$date = date("D d M Y, g:i a");
$sqlquery = "INSERT INTO tnews (Title, Body, Date) VALUES ('$title','$news','$date')";
header("Location: display.php");
//print $sqlquery;
$results = mysql_query($sqlquery);
mysql_close($dbc);
?>
any help would be greatly appreciated. Thanks
Chris
I use:
function bb2html($text)
{
$trans = array("" => "<b>", "" => "</b>", "[u]" => "<u>", "[/u]" => "</u>",
"" => "<i>", "" => "</i>",
":-)" => "<img src=\"smile.gif\" title=\"smile :-)\">"
"" => "<table><tr><td>", "" => "</td></tr></table>"
);$translated = strtr($text, $trans);
return $translated;
}
Hope this gives you some idea.
Michal
<?php
include 'dbconnect.php';
function bb2html($text)
{
$trans = array("[b]" => "<b>", "[/b]" => "</b>",
"[u]" => "<u>", "[/u]" => "</u>",
"[i]" => "<i>", "[/i]" => "</i>",
":-)" => "<img src=\"smile.gif\" title=\"smile :-)\">"
"[quote]" => "<table><tr><td>", "[/quote]" => "</td></tr></table>"
);
$translated = strtr($text, $trans);
return $translated;
}
...
$title = $_POST['title'];
$title = preg_replace($pattern, $replace, $title);
$news = preg_replace($pattern, $replace, $news);
$title = bb2html($title);
$news = bb2html($news);
$title = mysql_real_escape_string($title);
$news = mysql_real_escape_string(nl2br($news));
...
?>
Sorry, but in the previous message I forgot to unset bbcode translation on this forum
Is this sufficient?
Regards
Michal
// write new content to file
if ($post_content = $_POST['textarea']) {
$new_content = trim($post_content);
$new_content = htmlentities($new_content, ENT_NOQUOTES); // must dissable before converting bbcode to html
$new_content = replaceTags($new_content); // now our code works and the users does not
putFile($filename, $new_content);
}
// display content
$lines = getFile($filename);
$content = getEditable($lines);
$content = html_entity_decode($content); // must enable html to swap tags
$content = swapTags($content);
$content = htmlentities($content, ENT_NOQUOTES); // dissable html again
insertForm($content, $linkSet, $editPage);
}
// get editable content
function getEditable($lines) {
// Loop through array
foreach ($lines as $key => $line) {
if (preg_match('/<!\-\- TemplateBeginEditable name="Content" \-\->/', $line)) {
$editable = "true";
continue;
}
if (preg_match('/<!\-\- TemplateEndEditable \-\->/', $line)) {
$editable = "false";
}
if ($editable == "true") {
$content .= $line;
}
}
return $content;
}
function replaceTags($content) {
$patterns[0] = '/\[b\]/';
$patterns[1] = '/\[\/b\]/';
$patterns[2] = '/\[u\]/';
$patterns[3] = '/\[\/u\]/';
$patterns[4] = '/\[i\]/';
$patterns[5] = '/\[\/i\]/';
$patterns[6] = '/\[color\=("¦\')?(\w+)("¦\')?\]/';
$patterns[7] = '/\[size\=("¦\')?(\d+)("¦\')?\]/';
$patterns[8] = '/\[\/color\]/';
$patterns[9] = '/\[\/size\]/';
$patterns[10] = Chr(13).Chr(10);
$patterns[11] = '/\r?\n¦\r/';
$patterns[12] = '/\[url=("¦\')?(http[s]?:\/\/[^\]]+)("¦\')?\]([^\[]+)\[\/url\]/';
$replacements[0] = '<b>';
$replacements[1] = '</b>';
$replacements[2] = '<u>';
$replacements[3] = '</u>';
$replacements[4] = '<i>';
$replacements[5] = '</i>';
$replacements[6] = '<font color=$1$2$3>';
$replacements[7] = '<font size=$1$2$3>';
$replacements[8] = '</font><!-- /color -->';
$replacements[9] = '</font><!-- /size -->';
$replacements[10] = '<br />';
$replacements[11] = '<br />';
$replacements[12] = '<a href=$1$2$3>$4</a>';
ksort($patterns);
ksort($replacements);
$content = preg_replace($patterns, $replacements, $content);
// this must come after preg_replace to preserve the newline characters
$content = '<!-- TemplateBeginEditable name="Content" -->' . Chr(13) . Chr(10) . $content . Chr(13) . Chr(10) . '<!-- TemplateEndEditable -->';
return $content;
}
function swapTags($content) {
$patterns[0] = '/<b>/';
$patterns[1] = '/<\/b>/';
$patterns[2] = '/<u>/';
$patterns[3] = '/<\/u>/';
$patterns[4] = '/<i>/';
$patterns[5] = '/<\/i>/';
$patterns[6] = '/<font color=("¦\')?(\w+)("¦\')?>/';
$patterns[7] = '/<font size=("¦\')?(\d+)("¦\')?>/';
$patterns[8] = '/<\/font><!\-\- \/color \-\->/';
$patterns[9] = '/<\/font><!\-\- \/size \-\->/';
$patterns[10] = '/<br \/>/';
$patterns[11] = '/<a href=("¦\')?(http[s]?:\/\/.*)("¦\')?>(.+)<\/a>/';
$replacements[0] = '[b]';
$replacements[1] = '[/b]';
$replacements[2] = '[u]';
$replacements[3] = '[/u]';
$replacements[4] = '[i]';
$replacements[5] = '[/i]';
$replacements[6] = '[color=$1$2$3]';
$replacements[7] = '[size=$1$2$3]';
$replacements[8] = '[/color]';
$replacements[9] = '[/size]';
$replacements[10] = Chr(13).Chr(10);
$replacements[11] = '[url=$1$2$3]$4[/url]';
ksort($patterns);
ksort($replacements);
$content = preg_replace($patterns, $replacements, $content);
return $content;
}
[edited by: Stuperfied at 12:36 am (utc) on Dec. 7, 2006]