Created
March 25, 2013 19:44
-
-
Save benplum/5240033 to your computer and use it in GitHub Desktop.
Random HTML string functions
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
function splitFirstP($html) { | |
$parts = explode("</p>", $html, 2); | |
$parts[0] = $parts[0]."</p>"; | |
return $parts; | |
} | |
function splitCenterContent($html) { | |
$return = array(); | |
$parts = explode("</p>", $html); | |
$middle = floor(count($parts) / 2) - 1; | |
$j = 0; | |
for ($i = 0; $i < count($parts); $i++) { | |
$return[$j] .= $parts[$i]."</p>"; | |
if ($i == $middle) { | |
$j++; | |
} | |
} | |
return $return; | |
} | |
function splitAtP($html, $middle) { | |
$return = array(); | |
$parts = explode("</p>", $html); | |
$count = count($parts); | |
if ($middle < $count) { | |
$middle = $middle - 1; | |
$j = 0; | |
for ($i = 0; $i < $count; $i++) { | |
$return[$j] .= $parts[$i]."</p>"; | |
if ($i == $middle) { | |
$j++; | |
} | |
} | |
return $return; | |
} | |
return array($html, ""); | |
} | |
function trimFirstP($html, $count, $append = "") { | |
$html = getFirstP($html, $append); | |
$html = BigTree::trimLength($html, $count); | |
$html = str_ireplace("</p>", $append."</p>", $html); | |
return $html; | |
} | |
function getFirstP($html) { | |
$start = strpos($html, '<p>'); | |
$end = strpos($html, '</p>', $start); | |
$html = substr($html, $start, ($end - $start + 4)); | |
return $html; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment