pouet.net/include/libbb.php
2013-05-07 14:20:08 -04:00

729 lines
25 KiB
PHP

<?
/**
* bbdecode/bbencode functions:
* Rewritten - Nathan Codding - Aug 24, 2000
* quote, code, and list rewritten again in Jan. 2001.
* All BBCode tags now implemented. Nesting and multiple occurances should be
* handled fine for all of them. Using str_replace() instead of regexps often
* for efficiency. quote, list, and code are not regular, so they are
* implemented as PDAs - probably not all that efficient, but that's the way it is.
*
* Note: all BBCode tags are case-insensitive.
*/
function bbencode($message, $is_html_disabled) {
global $user;
// pad it with a space so we can distinguish between FALSE and matching the 1st char (index 0).
// This is important; bbencode_quote(), bbencode_list(), and bbencode_code() all depend on it.
$message = " " . $message;
// First: If there isn't a "[" and a "]" in the message, don't bother.
if (! (strpos($message, "[") && strpos($message, "]")) )
{
// Remove padding, return.
$message = substr($message, 1);
return $message;
}
// [CODE] and [/CODE] for posting code (HTML, PHP, C etc etc) in your posts.
$message = str_replace(array("\r\n", "\r", "\n"), "<br />", bbencode_code($message, $is_html_disabled));
// [QUOTE] and [/QUOTE] for posting replies with quote, or just for quoting stuff.
$message = bbencode_quote($message);
// [list] and [list=x] for (un)ordered lists.
$message = bbencode_list($message);
// [b] and [/b] for bolding text.
$message = preg_replace("/\[b\](.*?)\[\/b\]/si", "<!-- BBCode Start --><B>\\1</B><!-- BBCode End -->", $message);
// [i] and [/i] for italicizing text.
$message = preg_replace("/\[i\](.*?)\[\/i\]/si", "<!-- BBCode Start --><I>\\1</I><!-- BBCode End -->", $message);
// [u] and [/u] for underlining text.
$message = preg_replace("/\[u\](.*?)\[\/u\]/si", "<!-- BBCode Start --><U>\\1</U><!-- BBCode End -->", $message);
// [img]image_url_here[/img] code..
// echo "<!--";
// var_dump($user);
// echo "-->";
$ie7 = "<!-- BBCode Start --><IMG style='max-width:75%;' SRC=\"\\1\" BORDER=\"0\"><!-- BBCode End -->";
$ff3 = "<!-- BBCode Start --><div class='bbimage'><IMG SRC=\"\\1\" BORDER=\"0\"></div><!-- BBCode End -->";
//$test = "<!-- BBCode Start --><IMG style='max-width:95%; height=auto;'SRC=\"\\1\" BORDER=\"0\"><!-- BBCode End -->";
//$rplac = $ie7;
$rplac = "<!-- BBCode Start --><IMG class='bbimage' SRC=\"\\1\" BORDER=\"0\"><!-- BBCode End -->";
if($user["displayimages"] || $_GET["forceimages"])
$message = preg_replace("/\[img\](.*?)\[\/img\]/si", $rplac, $message);
else
$message = preg_replace("/\[img\](.*?)\[\/img\]/si", "<!-- BBCode Start --><a href=\"\\1\" style='font-size:90%'>[image disabled]</a> <!-- BBCode End -->", $message);
// Patterns and replacements for URL and email tags..
$patterns = array();
$replacements = array();
// [url]xxxx://www.phpbb.com[/url] code..
$patterns[0] = "#\[url\]([a-z]+?://){1}(.*?)\[/url\]#si";
$replacements[0] = '<!-- BBCode u1 Start --><A HREF="\1\2">\1\2</A><!-- BBCode u1 End -->';
// [url]www.phpbb.com[/url] code.. (no xxxx:// prefix).
$patterns[1] = "#\[url\](.*?)\[/url\]#si";
$replacements[1] = '<!-- BBCode u1 Start --><A HREF="http://\1">\1</A><!-- BBCode u1 End -->';
// [url=xxxx://www.phpbb.com]phpBB[/url] code..
$patterns[2] = "#\[url=([a-z]+?://){1}(.*?)\](.*?)\[/url\]#si";
$replacements[2] = '<!-- BBCode u2 Start --><A HREF="\1\2">\3</A><!-- BBCode u2 End -->';
// [url=www.phpbb.com]phpBB[/url] code.. (no xxxx:// prefix).
$patterns[3] = "#\[url=(.*?)\](.*?)\[/url\]#si";
$replacements[3] = '<!-- BBCode u2 Start --><A HREF="http://\1">\2</A><!-- BBCode u2 End -->';
// [email]user@domain.tld[/email] code..
$patterns[4] = "#\[email\](.*?)\[/email\]#si";
$replacements[4] = '<!-- BBCode Start --><A HREF="mailto:\1">\1</A><!-- BBCode End -->';
$message = preg_replace($patterns, $replacements, $message);
// Remove our padding from the string..
$message = substr($message, 1);
return $message;
} // bbencode()
function bbdecode($message) {
// Undo [code]
// $code_start_html = "<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD>Code:<HR></TD></TR><TR><TD><PRE>";
// $code_end_html = "</PRE></TD></TR><TR><TD><HR></TD></TR></TABLE><!-- BBCode End -->";
$code_start_html = "<!-- BBCode Start --><div class=\"bbs_code\"><b>Code:</b><pre>";
$code_end_html = "</pre></div><!-- BBCode End -->";
$message = str_replace($code_start_html, "[code]", $message);
$message = str_replace($code_end_html, "[/code]", $message);
// Undo [quote]
// $quote_start_html = "<!-- BBCode Quote Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD>Quote:<HR></TD></TR><TR><TD><BLOCKQUOTE>";
// $quote_end_html = "</BLOCKQUOTE></TD></TR><TR><TD><HR></TD></TR></TABLE><!-- BBCode Quote End -->";
$quote_start_html = "<!-- BBCode Quote Start --><div class=\"bbs_quote\"><b>Quote:</b><div>";
$quote_end_html = "</div></div><!-- BBCode Quote End -->";
$message = str_replace($quote_start_html, "[quote]", $message);
$message = str_replace($quote_end_html, "[/quote]", $message);
// Undo [b] and [i]
$message = preg_replace("#<!-- BBCode Start --><B>(.*?)</B><!-- BBCode End -->#s", "[b]\\1[/b]", $message);
$message = preg_replace("#<!-- BBCode Start --><I>(.*?)</I><!-- BBCode End -->#s", "[i]\\1[/i]", $message);
// Undo [url] (long form)
$message = preg_replace("#<!-- BBCode u2 Start --><A HREF=\"([a-z]+?://)(.*?)\">(.*?)</A><!-- BBCode u2 End -->#s", "[url=\\1\\2]\\3[/url]", $message);
// Undo [url] (short form)
$message = preg_replace("#<!-- BBCode u1 Start --><A HREF=\"([a-z]+?://)(.*?)\">(.*?)</A><!-- BBCode u1 End -->#s", "[url]\\3[/url]", $message);
// Undo [email]
$message = preg_replace("#<!-- BBCode Start --><A HREF=\"mailto:(.*?)\">(.*?)</A><!-- BBCode End -->#s", "[email]\\1[/email]", $message);
// Undo [img]
$message = preg_replace("#<!-- BBCode Start --><IMG SRC=\"(.*?)\" BORDER=\"0\"><!-- BBCode End -->#s", "[img]\\1[/img]", $message);
// Undo lists (unordered/ordered)
// <li> tags:
$message = str_replace("<!-- BBCode --><LI>", "[*]", $message);
// [list] tags:
$message = str_replace("<!-- BBCode ulist Start --><UL>", "[list]", $message);
// [list=x] tags:
$message = preg_replace("#<!-- BBCode olist Start --><OL TYPE=([A1])>#si", "[list=\\1]", $message);
// [/list] tags:
$message = str_replace("</UL><!-- BBCode ulist End -->", "[/list]", $message);
$message = str_replace("</OL><!-- BBCode olist End -->", "[/list]", $message);
return($message);
}
/**
* James Atkinson - Feb 5, 2001
* This function does exactly what the PHP4 function array_push() does
* however, to keep phpBB compatable with PHP 3 we had to come up with out own
* method of doing it.
*/
function bbcode_array_push(&$stack, $value) {
$stack[] = $value;
return(sizeof($stack));
}
/**
* James Atkinson - Feb 5, 2001
* This function does exactly what the PHP4 function array_pop() does
* however, to keep phpBB compatable with PHP 3 we had to come up with out own
* method of doing it.
*/
function bbcode_array_pop(&$stack) {
$arrSize = count($stack);
$x = 1;
while(list($key, $val) = each($stack)) {
if($x < count($stack)) {
$tmpArr[] = $val;
}
else {
$return_val = $val;
}
$x++;
}
$stack = $tmpArr;
return($return_val);
}
/**
* Nathan Codding - Jan. 12, 2001.
* Performs [quote][/quote] bbencoding on the given string, and returns the results.
* Any unmatched "[quote]" or "[/quote]" token will just be left alone.
* This works fine with both having more than one quote in a message, and with nested quotes.
* Since that is not a regular language, this is actually a PDA and uses a stack. Great fun.
*
* Note: This function assumes the first character of $message is a space, which is added by
* bbencode().
*/
function bbencode_quote($message)
{
// First things first: If there aren't any "[quote]" strings in the message, we don't
// need to process it at all.
if (!strpos(strtolower($message), "[quote]"))
{
return $message;
}
$stack = Array();
$curr_pos = 1;
while ($curr_pos && ($curr_pos < strlen($message)))
{
$curr_pos = strpos($message, "[", $curr_pos);
// If not found, $curr_pos will be 0, and the loop will end.
if ($curr_pos)
{
// We found a [. It starts at $curr_pos.
// check if it's a starting or ending quote tag.
$possible_start = substr($message, $curr_pos, 7);
$possible_end = substr($message, $curr_pos, 8);
if (strcasecmp("[quote]", $possible_start) == 0)
{
// We have a starting quote tag.
// Push its position on to the stack, and then keep going to the right.
bbcode_array_push($stack, $curr_pos);
++$curr_pos;
}
else if (strcasecmp("[/quote]", $possible_end) == 0)
{
// We have an ending quote tag.
// Check if we've already found a matching starting tag.
if (sizeof($stack) > 0)
{
// There exists a starting tag.
// We need to do 2 replacements now.
$start_index = bbcode_array_pop($stack);
// everything before the [quote] tag.
$before_start_tag = substr($message, 0, $start_index);
// everything after the [quote] tag, but before the [/quote] tag.
$between_tags = substr($message, $start_index + 7, $curr_pos - $start_index - 7);
// everything after the [/quote] tag.
$after_end_tag = substr($message, $curr_pos + 8);
$message = $before_start_tag . "<!-- BBCode Quote Start --><div class=\"bbs_quote\"><b>Quote:</b><div>";
$message .= $between_tags . "</div></div><!-- BBCode Quote End -->";
$message .= $after_end_tag;
// Now.. we've screwed up the indices by changing the length of the string.
// So, if there's anything in the stack, we want to resume searching just after it.
// otherwise, we go back to the start.
if (sizeof($stack) > 0)
{
$curr_pos = bbcode_array_pop($stack);
bbcode_array_push($stack, $curr_pos);
++$curr_pos;
}
else
{
$curr_pos = 1;
}
}
else
{
// No matching start tag found. Increment pos, keep going.
++$curr_pos;
}
}
else
{
// No starting tag or ending tag.. Increment pos, keep looping.,
++$curr_pos;
}
}
} // while
return $message;
} // bbencode_quote()
/**
* Nathan Codding - Jan. 12, 2001.
* Performs [code][/code] bbencoding on the given string, and returns the results.
* Any unmatched "[code]" or "[/code]" token will just be left alone.
* This works fine with both having more than one code block in a message, and with nested code blocks.
* Since that is not a regular language, this is actually a PDA and uses a stack. Great fun.
*
* Note: This function assumes the first character of $message is a space, which is added by
* bbencode().
*/
function bbencode_code($message, $is_html_disabled)
{
// First things first: If there aren't any "[code]" strings in the message, we don't
// need to process it at all.
if (!strpos(strtolower($message), "[code]"))
{
return $message;
}
//return preg_replace("/\[code\](.*?)\[\/code\]/si", "<!-- BBCode Start --><div class=\"bbs_code\"><b>Code:</b><pre>\1</pre></div><!-- BBCode End -->", $message);
// $message = preg_replace("/$str_to_match/si", "<!-- BBCode Start --><div class=\"bbs_code\"><b>Code:</b><pre>$after_replace</pre></div><!-- BBCode End -->", $message);
// Second things second: we have to watch out for stuff like [1code] or [/code1] in the
// input.. So escape them to [#1code] or [/code#1] for now:
$message = preg_replace("/\[([0-9]+?)code\]/si", "[#\\1code]", $message);
$message = preg_replace("/\[\/code([0-9]+?)\]/si", "[/code#\\1]", $message);
$stack = Array();
$curr_pos = 1;
$max_nesting_depth = 0;
while ($curr_pos && ($curr_pos < strlen($message)))
{
$curr_pos = strpos($message, "[", $curr_pos);
// If not found, $curr_pos will be 0, and the loop will end.
if ($curr_pos)
{
// We found a [. It starts at $curr_pos.
// check if it's a starting or ending code tag.
$possible_start = substr($message, $curr_pos, 6);
$possible_end = substr($message, $curr_pos, 7);
if (strcasecmp("[code]", $possible_start) == 0)
{
// We have a starting code tag.
// Push its position on to the stack, and then keep going to the right.
bbcode_array_push($stack, $curr_pos);
++$curr_pos;
}
else if (strcasecmp("[/code]", $possible_end) == 0)
{
// We have an ending code tag.
// Check if we've already found a matching starting tag.
if (sizeof($stack) > 0)
{
// There exists a starting tag.
$curr_nesting_depth = sizeof($stack);
$max_nesting_depth = ($curr_nesting_depth > $max_nesting_depth) ? $curr_nesting_depth : $max_nesting_depth;
// We need to do 2 replacements now.
$start_index = bbcode_array_pop($stack);
// everything before the [code] tag.
$before_start_tag = substr($message, 0, $start_index);
// everything after the [code] tag, but before the [/code] tag.
$between_tags = substr($message, $start_index + 6, $curr_pos - $start_index - 6);
// everything after the [/code] tag.
$after_end_tag = substr($message, $curr_pos + 7);
$message = $before_start_tag . "[" . $curr_nesting_depth . "code]";
$message .= $between_tags . "[/code" . $curr_nesting_depth . "]";
$message .= $after_end_tag;
// Now.. we've screwed up the indices by changing the length of the string.
// So, if there's anything in the stack, we want to resume searching just after it.
// otherwise, we go back to the start.
if (sizeof($stack) > 0)
{
$curr_pos = bbcode_array_pop($stack);
bbcode_array_push($stack, $curr_pos);
++$curr_pos;
}
else
{
$curr_pos = 1;
}
}
else
{
// No matching start tag found. Increment pos, keep going.
++$curr_pos;
}
}
else
{
// No starting tag or ending tag.. Increment pos, keep looping.,
++$curr_pos;
}
}
} // while
//echo "<!--".$message."-->";
if ($max_nesting_depth > 0)
{
for ($i = 1; $i <= $max_nesting_depth; ++$i)
{
$start_tag = escape_slashes(preg_quote("[" . $i . "code]"));
$end_tag = escape_slashes(preg_quote("[/code" . $i . "]"));
$match_count = preg_match_all("/$start_tag(.*?)$end_tag/si", $message, $matches);
for ($j = 0; $j < $match_count; $j++)
{
$before_replace = escape_slashes(preg_quote($matches[1][$j]));
//debuglog($before_replace);
$after_replace = preg_quote($matches[1][$j]);
//debuglog($after_replace);
if (($i < 2) && !$is_html_disabled)
{
// don't escape special chars when we're nested, 'cause it was already done
// at the lower level..
// also, don't escape them if HTML is disabled in this post. it'll already be done
// by the posting routines.
$after_replace = htmlspecialchars($after_replace);
}
$str_to_match = $start_tag . $before_replace . $end_tag;
$message = preg_replace("/".$str_to_match."/si", "<!-- BBCode Start --><div class=\"bbs_code\"><b>Code:</b><pre>".$after_replace."</pre></div><!-- BBCode End -->", $message);
}
}
}
// Undo our escaping from "second things second" above..
$message = preg_replace("/\[#([0-9]+?)code\]/si", "[\\1code]", $message);
$message = preg_replace("/\[\/code#([0-9]+?)\]/si", "[/code\\1]", $message);
return $message;
} // bbencode_code()
/**
* Nathan Codding - Jan. 12, 2001.
* Performs [list][/list] and [list=?][/list] bbencoding on the given string, and returns the results.
* Any unmatched "[list]" or "[/list]" token will just be left alone.
* This works fine with both having more than one list in a message, and with nested lists.
* Since that is not a regular language, this is actually a PDA and uses a stack. Great fun.
*
* Note: This function assumes the first character of $message is a space, which is added by
* bbencode().
*/
function bbencode_list($message)
{
$start_length = Array();
$start_length[ordered] = 8;
$start_length[unordered] = 6;
// First things first: If there aren't any "[list" strings in the message, we don't
// need to process it at all.
if (!strpos(strtolower($message), "[list"))
{
return $message;
}
$stack = Array();
$curr_pos = 1;
while ($curr_pos && ($curr_pos < strlen($message)))
{
$curr_pos = strpos($message, "[", $curr_pos);
// If not found, $curr_pos will be 0, and the loop will end.
if ($curr_pos)
{
// We found a [. It starts at $curr_pos.
// check if it's a starting or ending list tag.
$possible_ordered_start = substr($message, $curr_pos, $start_length[ordered]);
$possible_unordered_start = substr($message, $curr_pos, $start_length[unordered]);
$possible_end = substr($message, $curr_pos, 7);
if (strcasecmp("[list]", $possible_unordered_start) == 0)
{
// We have a starting unordered list tag.
// Push its position on to the stack, and then keep going to the right.
bbcode_array_push($stack, array($curr_pos, ""));
++$curr_pos;
}
else if (preg_match("/\[list=([a1])\]/si", $possible_ordered_start, $matches))
{
// We have a starting ordered list tag.
// Push its position on to the stack, and the starting char onto the start
// char stack, the keep going to the right.
bbcode_array_push($stack, array($curr_pos, $matches[1]));
++$curr_pos;
}
else if (strcasecmp("[/list]", $possible_end) == 0)
{
// We have an ending list tag.
// Check if we've already found a matching starting tag.
if (sizeof($stack) > 0)
{
// There exists a starting tag.
// We need to do 2 replacements now.
$start = bbcode_array_pop($stack);
$start_index = $start[0];
$start_char = $start[1];
$is_ordered = ($start_char != "");
$start_tag_length = ($is_ordered) ? $start_length[ordered] : $start_length[unordered];
// everything before the [list] tag.
$before_start_tag = substr($message, 0, $start_index);
// everything after the [list] tag, but before the [/list] tag.
$between_tags = substr($message, $start_index + $start_tag_length, $curr_pos - $start_index - $start_tag_length);
// Need to replace [*] with <LI> inside the list.
$between_tags = str_replace("[*]", "<!-- BBCode --><LI>", $between_tags);
// everything after the [/list] tag.
$after_end_tag = substr($message, $curr_pos + 7);
if ($is_ordered)
{
$message = $before_start_tag . "<!-- BBCode olist Start --><OL TYPE=" . $start_char . ">";
$message .= $between_tags . "</OL><!-- BBCode olist End -->";
}
else
{
$message = $before_start_tag . "<!-- BBCode ulist Start --><UL>";
$message .= $between_tags . "</UL><!-- BBCode ulist End -->";
}
$message .= $after_end_tag;
// Now.. we've screwed up the indices by changing the length of the string.
// So, if there's anything in the stack, we want to resume searching just after it.
// otherwise, we go back to the start.
if (sizeof($stack) > 0)
{
$a = bbcode_array_pop($stack);
$curr_pos = $a[0];
bbcode_array_push($stack, $a);
++$curr_pos;
}
else
{
$curr_pos = 1;
}
}
else
{
// No matching start tag found. Increment pos, keep going.
++$curr_pos;
}
}
else
{
// No starting tag or ending tag.. Increment pos, keep looping.,
++$curr_pos;
}
}
} // while
return $message;
} // bbencode_list()
/**
* Nathan Codding - Oct. 30, 2000
*
* Escapes the "/" character with "\/". This is useful when you need
* to stick a runtime string into a PREG regexp that is being delimited
* with slashes.
*/
function escape_slashes($input)
{
$output = str_replace('/', '\/', $input);
//$output = str_replace('$', '\$', $input);
return $output;
}
/**
* Rewritten by Nathan Codding - Feb 6, 2001.
* - Goes through the given string, and replaces xxxx://yyyy with an HTML <a> tag linking
* to that URL
* - Goes through the given string, and replaces www.xxxx.yyyy[zzzz] with an HTML <a> tag linking
* to http://www.xxxx.yyyy[/zzzz]
* - Goes through the given string, and replaces xxxx@yyyy with an HTML mailto: tag linking
* to that email address
* - Only matches these 2 patterns either after a space, or at the beginning of a line
*
* Notes: the email one might get annoying - it's easy to make it more restrictive, though.. maybe
* have it require something like xxxx@yyyy.zzzz or such. We'll see.
*/
function make_clickable($text) {
// pad it with a space so we can match things at the start of the 1st line.
$ret = " " . $text;
// matches an "xxxx://yyyy" URL at the start of a line, or after a space.
// xxxx can only be alpha characters.
// yyyy is anything up to the first space, newline, or comma.
$ret = preg_replace("#([\n ])([a-z]+?)://([^, \n\r]+)#i", "\\1<!-- BBCode auto-link start --><a href=\"\\2://\\3\">\\2://\\3</a><!-- BBCode auto-link end -->", $ret);
// matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing
// Must contain at least 2 dots. xxxx contains either alphanum, or "-"
// yyyy contains either alphanum, "-", or "."
// zzzz is optional.. will contain everything up to the first space, newline, or comma.
// This is slightly restrictive - it's not going to match stuff like "forums.foo.com"
// This is to keep it from getting annoying and matching stuff that's not meant to be a link.
$ret = preg_replace("#([\n ])www\.([a-z0-9\-]+)\.([a-z0-9\-.\~]+)((?:/[^, \n\r]*)?)#i", "\\1<!-- BBCode auto-link start --><a href=\"http://www.\\2.\\3\\4\">www.\\2.\\3\\4</a><!-- BBCode auto-link end -->", $ret);
// matches an email@domain type address at the start of a line, or after a space.
// Note: before the @ sign, the only valid characters are the alphanums and "-", "_", or ".".
// After the @ sign, we accept anything up to the first space, linebreak, or comma.
$ret = preg_replace("#([\n ])([a-z0-9\-_.]+?)@([^, \n\r]+)#i", "\\1<!-- BBcode auto-mailto start --><a href=\"mailto:\\2@\\3\">\\2@\\3</a><!-- BBCode auto-mailto end -->", $ret);
// Remove our padding..
$ret = substr($ret, 1);
return($ret);
}
/**
* Nathan Codding - Feb 6, 2001
* Reverses the effects of make_clickable(), for use in editpost.
* - Does not distinguish between "www.xxxx.yyyy" and "http://aaaa.bbbb" type URLs.
*
*/
function undo_make_clickable($text) {
$text = preg_replace("#<!-- BBCode auto-link start --><a href=\"(.*?)\">.*?</a><!-- BBCode auto-link end -->#i", "\\1", $text);
$text = preg_replace("#<!-- BBcode auto-mailto start --><a href=\"mailto:(.*?)\">.*?</a><!-- BBCode auto-mailto end -->#i", "\\1", $text);
return $text;
}
/**
* Nathan Codding - August 24, 2000.
* Takes a string, and does the reverse of the PHP standard function
* htmlspecialchars().
*/
function undo_htmlspecialchars($input) {
$input = preg_replace("/&gt;/i", ">", $input);
$input = preg_replace("/&lt;/i", "<", $input);
$input = preg_replace("/&quot;/i", "\"", $input);
$input = preg_replace("/&amp;/i", "&", $input);
return $input;
}
/**
* Translates any sequence of whitespace (\t, \r, \n, or space) in the given
* string into a single space character.
* Returns the result.
*/
function normalize_whitespace($str)
{
$output = "";
$tok = preg_split("/[ \t\r\n]+/", $str);
$tok_count = sizeof($tok);
for ($i = 0; $i < ($tok_count - 1); $i++)
{
$output .= $tok[$i] . " ";
}
$output .= $tok[$tok_count - 1];
return $output;
}
/**
* Less agressive version of stripslashes. Only replaces \\ \' and \"
* The PHP stripslashes() also removed single backslashes from the string.
* Expects a string or array as an argument.
* Returns the result.
*/
function own_stripslashes($string)
{
$find = array(
'/\\\\\'/', // \\\'
'/\\\\/', // \\
'/\\\'/', // \'
'/\\\"/'); // \"
$replace = array(
'\'', // \
'\\', // \
'\'', // '
'"'); // "
return preg_replace($find, $replace, $string);
}
function htmlcleanonerow($inhtml){
$inhtml= str_replace( "<", "&" . "lt;", $inhtml);
$inhtml= str_replace( ">", "&" . "gt;", $inhtml);
$inhtml= str_replace( "\"", "&" . "quot;", $inhtml);
$inhtml= str_replace( "\n", " ", $inhtml);
return $inhtml;
}
function parseUrl( $str, $splitPosition=56 )
/*
* /!\ it assumes that $str went through the htmlentities( ) function.
*/
{
$textWithATags = preg_replace( "(([a-zA-Z]+://)([\\S\"']+))", "<a href='\\1\\2' rel='external'>link me beautiful</a>", $str );
if( $splitPosition )
{
$str = "";
$i = 0;
$tagOpen = 0;
while( strlen( $textWithATags ) )
{
$currentChar = substr( $textWithATags, 0, 1 );
$str .= $currentChar;
$textWithATags = substr( $textWithATags, 1 );
if( $currentChar=="<" )
$tagOpen++;
if( $tagOpen==0 )
$i = $currentChar==" "?0:$i+1;
if( $currentChar==">" )
$tagOpen--;
if( $i==$splitPosition )
{
$i = 0;
$str .= "<br />";
}
}
return $str;
}
return $textWithATags;
}
?>