Skip to content

Instantly share code, notes, and snippets.

@Sama34
Created April 7, 2014 04:59
Show Gist options
  • Save Sama34/10015046 to your computer and use it in GitHub Desktop.
Save Sama34/10015046 to your computer and use it in GitHub Desktop.
OUGC Count Characters Function
<?php
/***************************************************************************
*
* OUGC Count Characters Function
* Author: Omar Gonzalez
* Copyright: © 2013-2014 Omar Gonzalez
*
* Website: http://omarg.me
*
* Counts a message's characters removing special code.
*
***************************************************************************
****************************************************************************
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
/**
* Counts a message's characters removing special code.
* Based off Zinga Burga's "Thread Tooltip Preview" plugin threadtooltip_getpreview() function.
*
* @param string Message to count.
* @return integer Characters count
**/
function ougc_countchars($message)
{
// Attempt to remove any quotes
$message = preg_replace(array(
'#\[quote=([\"\']|&quot;|)(.*?)(?:\\1)(.*?)(?:[\"\']|&quot;)?\](.*?)\[/quote\](\r\n?|\n?)#esi',
'#\[quote\](.*?)\[\/quote\](\r\n?|\n?)#si',
'#\[quote\]#si',
'#\[\/quote\]#si'
), '', $message);
// Attempt to remove any MyCode
global $parser;
if(!is_object($parser))
{
require_once MYBB_ROOT.'inc/class_parser.php';
$parser = new postParser;
}
$message = $parser->parse_message($message, array(
'allow_html' => 0,
'allow_mycode' => 1,
'allow_smilies' => 0,
'allow_imgcode' => 1,
'filter_badwords' => 1,
'nl2br' => 0
));
// before stripping tags, try converting some into spaces
$message = preg_replace(array(
'~\<(?:img|hr).*?/\>~si',
'~\<li\>(.*?)\</li\>~si'
), array(' ', "\n* $1"), $message);
$message = unhtmlentities(strip_tags($message));
// Remove all spaces?
$message = trim_blank_chrs($message);
$message = preg_replace('/\s+/', '', $message);
// convert \xA0 to spaces (reverse &nbsp;)
$message = trim(preg_replace(array('~ {2,}~', "~\n{2,}~"), array(' ', "\n"), strtr($message, array("\xA0" => ' ', "\r" => '', "\t" => ' '))));
// newline fix for browsers which don't support them
$message = preg_replace("~ ?\n ?~", " \n", $message);
return (int)my_strlen($message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment