Last active
August 29, 2015 14:27
-
-
Save MattLoyeD/b42acdbf71b70201f251 to your computer and use it in GitHub Desktop.
Easy substr for paragraph in PHP
This file contains hidden or 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 | |
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