Easy substr for paragraph in PHP
<?php | |
function substr_paragraph($string, $length = 520, $html = false) { | |
$pos = strpos($string, "</p>",$length); | |
$tag_len = 4; | |
if(empty($pos)){ | |
$pos = strpos($string, "</div>",$length); | |
$tag_len = 5; | |
} | |
if(empty($pos)){ | |
// No usable html found | |
$pos = $length-$tag_len; | |
} | |
$string = substr($string,0, $pos+$tag_len); | |
if($html != true){ | |
$string = strip_tags($string); | |
} | |
return $string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment