Last active
August 29, 2015 13:58
-
-
Save Sama34/10012654 to your computer and use it in GitHub Desktop.
OUGC Get Preview Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*************************************************************************** | |
* | |
* OUGC Get Preview Function | |
* Author: Omar Gonzalez | |
* Copyright: © 2012-2014 Omar Gonzalez | |
* | |
* Website: http://omarg.me | |
* | |
* Shorts a message to look like a preview. | |
* | |
*************************************************************************** | |
**************************************************************************** | |
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/>. | |
****************************************************************************/ | |
/** | |
* Shorts a message to look like a preview. | |
* Based off Zinga Burga's "Thread Tooltip Preview" plugin threadtooltip_getpreview() function. | |
* | |
* @param string Message to short. | |
* @param int Maximum characters to show. | |
* @param bool Strip MyCode Quotes from message. | |
* @param bool Strip MyCode from message. | |
* @return string Shortened message | |
**/ | |
function ougc_getpreview($message, $maxlen=100, $stripquotes=true, $stripmycode=true) | |
{ | |
// Attempt to remove quotes, skip if going to strip MyCode | |
if($stripquotes && !$stripmycode) | |
{ | |
$message = preg_replace(array( | |
'#\[quote=([\"\']|"|)(.*?)(?:\\1)(.*?)(?:[\"\']|")?\](.*?)\[/quote\](\r\n?|\n?)#esi', | |
'#\[quote\](.*?)\[\/quote\](\r\n?|\n?)#si', | |
'#\[quote\]#si', | |
'#\[\/quote\]#si' | |
), '', $message); | |
} | |
// Attempt to remove any MyCode | |
if($stripmycode) | |
{ | |
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)); | |
} | |
// convert \xA0 to spaces (reverse ) | |
$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); | |
// Shorten the message if too long | |
if(my_strlen($message) > $maxlen) | |
{ | |
$message = my_substr($message, 0, $maxlen-1).'...'; | |
} | |
return htmlspecialchars_uni($message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment