Created
July 6, 2016 11:29
-
-
Save devudit/aa7adc2e63dc0df677a8ad6ecbf42602 to your computer and use it in GitHub Desktop.
Truncate text without truncating html
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 | |
/* Plugin for expression engine 2 */ | |
if (!defined('BASEPATH')) exit('No direct script access allowed'); | |
/* | |
======================================================== | |
Plugin Eme Utills | |
-------------------------------------------------------- | |
Copyright: Udit Rawat | |
License: Freeware | |
-------------------------------------------------------- | |
This addon may be used free of charge. Should you | |
employ it in a commercial project of a customer or your | |
own I'd appreciate a small donation. | |
======================================================== | |
File: pi.eme_utills.php | |
-------------------------------------------------------- | |
Purpose: Special function supporting ee templates | |
======================================================== | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF | |
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT | |
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO | |
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE | |
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | |
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE | |
OR OTHER DEALINGS IN THE SOFTWARE. | |
======================================================== | |
*/ | |
$plugin_info = array( | |
'pi_name' => 'EME_Utills', | |
'pi_version' => '1.0', | |
'pi_author' => 'Udit Rawat', | |
'pi_author_url' => 'http://emerico.in', | |
'pi_description' => 'String / Array and some special function support for ee template', | |
'pi_usage' => EME_Utills::usage() | |
); | |
class EME_Utills | |
{ | |
public static function usage() | |
{ | |
ob_start(); | |
?> | |
Documentation available for developer only | |
<?php | |
$buffer = ob_get_contents(); | |
ob_end_clean(); | |
return $buffer; | |
} | |
function Trunchtml() | |
{ | |
$this->EE =& get_instance(); | |
$text = $this->EE->TMPL->tagdata; | |
$chars = $this->EE->TMPL->fetch_param('chars', '500'); | |
$ending = $this->EE->TMPL->fetch_param('ending', ''); | |
$isHTML = $this->EE->TMPL->fetch_param('html', true); | |
$text = trim(preg_replace('/\s\s+/', ' ', $text)); | |
$s = trim($text); | |
$l = $chars; | |
$e = (strlen(strip_tags($s)) > $chars) ? $ending : ''; | |
$i = 0; | |
$tags = array(); | |
if($isHTML) { | |
preg_match_all('/<[^>]+>([^<]*)/', $s, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); | |
foreach($m as $o) { | |
if($o[0][1] - $i >= $l) { | |
break; | |
} | |
$t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1); | |
if($t[0] != '/') { | |
$tags[] = $t; | |
} | |
elseif(end($tags) == substr($t, 1)) { | |
array_pop($tags); | |
} | |
$i += $o[1][1] - $o[0][1]; | |
} | |
} | |
$output = substr($s, 0, $l = min(strlen($s), $l + $i)) . (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '') . $e; | |
return $output; | |
} | |
} | |
/* | |
Usage: | |
{exp:fws_utills:trunchtml chars="50"} | |
{product_excerpt} | |
{/exp:fws_utills:trunchtml} |
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 | |
function truncate_html($s, $l, $e = '…', $isHTML = true) { | |
$s = trim($s); | |
$e = (strlen(strip_tags($s)) > $l) ? $e : ''; | |
$i = 0; | |
$tags = array(); | |
if($isHTML) { | |
preg_match_all('/<[^>]+>([^<]*)/', $s, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER); | |
foreach($m as $o) { | |
if($o[0][1] - $i >= $l) { | |
break; | |
} | |
$t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1); | |
if($t[0] != '/') { | |
$tags[] = $t; | |
} | |
elseif(end($tags) == substr($t, 1)) { | |
array_pop($tags); | |
} | |
$i += $o[1][1] - $o[0][1]; | |
} | |
} | |
$output = substr($s, 0, $l = min(strlen($s), $l + $i)) . (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '') . $e; | |
return $output; | |
} | |
/* | |
Usage: | |
truncate_html('<p>Smak av mango, limonade og brunt sukker</p> cool! it working for me', 24); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment