Skip to content

Instantly share code, notes, and snippets.

@MattLoyeD
Last active August 29, 2015 14:27
Show Gist options
  • Save MattLoyeD/b42acdbf71b70201f251 to your computer and use it in GitHub Desktop.
Save MattLoyeD/b42acdbf71b70201f251 to your computer and use it in GitHub Desktop.
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