Skip to content

Instantly share code, notes, and snippets.

@randomecho
Last active Dec 16, 2015
Embed
What would you like to do?
Strip down a chunk of formatted text to use for META description tag.
/**
* Render text with possible markup into snippet of plain text chunk
* suitable for meta description tag or other display area that
* may not like HTML tags or Markdown formatting.
*
* @author Soon Van - randomecho.com
* @copyright 2013 Soon Van
* @license http://www.opensource.org/licenses/BSD-3-Clause
*
* @param string raw text with possible formatting
* @param integer Max length of characters for the snippet
* @return string snippet version
*/
function snipper($text, $max_chars = 200)
{
// HTML & PHP tags are not needed
$text = strip_tags($text);
// Yank out the most likely used Markdown formatting that may appear
$regex_format[] = '/\*{2}([^\*]+)\*{2}/Ui'; // **strong**
$regex_format[] = '/[\*]{1}([^\*]+)[\*]{1}/Ui'; // *cite*
$regex_format[] = '/[_]{2}([^_]+)[_]{2}/Ui'; // __em__
$regex_format[] = '/[_]{1}([^_]+)[_]{1}/Ui'; // _cite_
$plain_version[] = "$1";
$plain_version[] = "$1";
$plain_version[] = "$1";
$plain_version[] = "$1";
$text = preg_replace($regex_format, $plain_version, $text);
// No time for Markdown links
$regex_link[] = '/\[(.+)\]\((\S*)\s"(.*)"\)/Ui';
$regex_link[] = '/\[(.+)\]\((\S*)\s"(.*)"(\s(.*))?\)/Ui';
$regex_link[] = '/\[(.+)\]\((\S*)\)/Ui';
$html_link[] = "$1";
$html_link[] = "$1";
$html_link[] = "$1";
$text = preg_replace($regex_link, $html_link, $text);
// There is no place for newlines in the meta tag
$text = str_replace("\r", "", $text);
$text = str_replace("\n", " ", $text);
$text = str_replace(" ", " ", $text);
$text = str_replace('"', '"', $text);
// Get rid of any leading or trailing spaces
$text = trim($text);
// Pare back to work from raw unformatted text at the max length
$text = substr($text, 0, $max_chars);
// See if we can get anything sensical in the last quarter
$front_chunk = $max_chars * 0.75;
$last_stop = strrpos(substr($text, $front_chunk), '.');
if ($last_stop === false)
{
// Full stop not found in last quarter, so trail off with ellipsis
$last_word = strrpos(substr($text, 0, $max_chars - 3), ' ');
$text = substr($text, 0, $last_word). ' ...';
}
else
{
// Or cut off at most logical sentence break found based on full stop
$snippet_stop = $front_chunk + $last_stop + 1;
$text = substr($text, 0, $snippet_stop);
}
return $text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment