Created
January 24, 2014 06:33
-
-
Save LeoOnTheEarth/8592992 to your computer and use it in GitHub Desktop.
A class to cut string that contains html tags
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 | |
/** | |
* Class HTMLCutter | |
*/ | |
class HTMLCutter | |
{ | |
/** | |
* Cut HTML | |
* | |
* @param string $text | |
* @param int $max_length | |
* | |
* @return string | |
*/ | |
public static function 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 < mb_strlen($text) && $stripped < mb_strlen($stripped_text) && $stripped < $max_length) | |
{ | |
$symbol = mb_substr($text, $i, 1); | |
$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 = ""; | |
} | |
elseif ($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