Skip to content

Instantly share code, notes, and snippets.

@yanknudtskov
Created January 28, 2021 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yanknudtskov/13f42e8efdb8bb650db4d3230dc853dd to your computer and use it in GitHub Desktop.
Save yanknudtskov/13f42e8efdb8bb650db4d3230dc853dd to your computer and use it in GitHub Desktop.
HTML cutter when you want to preserver formatting and avoid strip_tags and still be able to grab a substr (which can cut off HTML-tags)
<?php
function yanco_html_cut($text, $max_length)
{
$tags = array();
$result = "";
$is_open = false;
$grab_open = false;
$is_close = false;
$in_double_quotes = false;
$in_single_quotes = false;
$tag = "";
$i = 0;
$stripped = 0;
$stripped_text = strip_tags($text);
while ($i < strlen($text) && $stripped < strlen($stripped_text) && $stripped < $max_length) {
$symbol = $text{
$i};
$result .= $symbol;
switch ($symbol) {
case '<':
$is_open = true;
$grab_open = true;
break;
case '"':
if ($in_double_quotes)
$in_double_quotes = false;
else
$in_double_quotes = true;
break;
case "'":
if ($in_single_quotes)
$in_single_quotes = false;
else
$in_single_quotes = true;
break;
case '/':
if ($is_open && !$in_double_quotes && !$in_single_quotes) {
$is_close = true;
$is_open = false;
$grab_open = false;
}
break;
case ' ':
if ($is_open)
$grab_open = false;
else
$stripped++;
break;
case '>':
if ($is_open) {
$is_open = false;
$grab_open = false;
array_push($tags, $tag);
$tag = "";
} else if ($is_close) {
$is_close = false;
array_pop($tags);
$tag = "";
}
break;
default:
if ($grab_open || $is_close)
$tag .= $symbol;
if (!$is_open && !$is_close)
$stripped++;
}
$i++;
}
while ($tags)
$result .= "</" . array_pop($tags) . ">";
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment