Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

how do I remove html from a string?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
carl_galloway
Smarty n00b


Joined: 17 Feb 2006
Posts: 3

PostPosted: Tue Mar 28, 2006 3:06 am    Post subject: how do I remove html from a string? Reply with quote

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
View user's profile Send private message Visit poster's website
TGKnIght
Smarty Junkie


Joined: 07 Sep 2005
Posts: 580
Location: Philadelphia, PA

PostPosted: Tue Mar 28, 2006 2:44 pm    Post subject: Reply with quote

Try applying this modifier before your truncate :
http://smarty.php.net/manual/en/language.modifier.strip.tags.php
_________________
Smarty site with one index.php controller file
Working with MySQL and Smarty
SmartyColumnSort
Custom Smarty Javascript Debug Template
Back to top
View user's profile Send private message Visit poster's website
carl_galloway
Smarty n00b


Joined: 17 Feb 2006
Posts: 3

PostPosted: Wed Mar 29, 2006 12:57 am    Post subject: Reply with quote

TGKnIght, thank you, that works.

Cheers

Carl
Back to top
View user's profile Send private message Visit poster's website
myhujra
Smarty n00b


Joined: 17 Apr 2010
Posts: 1

PostPosted: Sat Apr 17, 2010 4:06 am    Post subject: Re: how do I remove html from a string? Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP