|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
carl_galloway Smarty n00b
Joined: 17 Feb 2006 Posts: 3
|
Posted: Tue Mar 28, 2006 3:06 am Post subject: how do I remove html from a string? |
|
|
Hi all, not sure if I'm asking the right wuestion so let me explain what I want to do. I'm developing a theme for Serendipity blog. An entry can contain html, for example a link or an image. What I want to do is truncate the entry body to just 60 or 70 characters, and "truncate:60" or similar works well for me, but when the entry contains any html it gets counted. Does anyone know how I could parse the string/variable and have Smarty remove or ignore any html that begins <img... or <a href...
Cheers
Carl |
|
Back to top |
|
TGKnIght Smarty Junkie
Joined: 07 Sep 2005 Posts: 580 Location: Philadelphia, PA
|
|
Back to top |
|
carl_galloway Smarty n00b
Joined: 17 Feb 2006 Posts: 3
|
Posted: Wed Mar 29, 2006 12:57 am Post subject: |
|
|
TGKnIght, thank you, that works.
Cheers
Carl |
|
Back to top |
|
myhujra Smarty n00b
Joined: 17 Apr 2010 Posts: 1
|
Posted: Sat Apr 17, 2010 4:06 am Post subject: Re: how do I remove html from a string? |
|
|
carl_galloway wrote: | . Does anyone know how I could parse the string/variable and have Smarty remove or ignore any html that begins <img... or <a href...
Carl |
Try this plugin I have found some where on web
<?php
/**
* Smarty plugin
* package Smarty
* subpackage plugins
*/
/**
* Smarty strip_tags modifier plugin
*
* Type: modifier
* Name: strip_tags
* Purpose: strip html tags from text
* strip_tags (Smarty online manual)
*
* @author Monte Ohrt <monte at ohrt dot com>
* @author Jordon Mears <jordoncm at gmail dot com>
*
* @version 2.0
*
* @param string
* @param boolean optional
* @param string optional
* @return string
*/
function smarty_modifier_strip_tags($string) {
switch(func_num_args()) {
case 1:
$replace_with_space = true;
break;
case 2:
$arg = func_get_arg(1);
if($arg === 1 || $arg === true || $arg === '1' || $arg === 'true') {
// for full legacy support || $arg === 'false' should be included
$replace_with_space = true;
$allowable_tags = '';
} elseif($arg === 0 || $arg === false || $arg === '0' || $arg === 'false') {
// for full legacy support || $arg === 'false' should be removed
$replace_with_space = false;
$allowable_tags = '';
} else {
$replace_with_space = true;
$allowable_tags = $arg;
}
break;
case 3:
$replace_with_space = func_get_arg(1);
$allowable_tags = func_get_arg(2);
break;
}
if($replace_with_space) {
$string = preg_replace('!(<[^>]*?>)!', '$1 ', $string);
}
$string = strip_tags($string, $allowable_tags);
if($replace_with_space) {
$string = preg_replace('!(<[^>]*?>) !', '$1', $string);
}
return $string;
}
/* vim: set expandtab: */
?> _________________ www.MyHujra.com |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|