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

A br2nl modifier.

 
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 -> Plugins
View previous topic :: View next topic  
Author Message
naish
Smarty n00b


Joined: 13 May 2003
Posts: 4

PostPosted: Tue May 13, 2003 9:26 am    Post subject: A br2nl modifier. Reply with quote

Browsing the source code I wondered why a br2nl function was missing, so I whipped one up.

[php:1:c3bb6390f0]<?php

/*
* Smarty plugin
* -------------------------------------------------------------
* Type: modifier
* Name: br2nl
* Version: 1.0
* Date: May 13, 2003
* Author: Michael Wulff Nielsen
* Based on: nl2br by Monte Ohrt <monte@ispi.net>
* Purpose: convert <br>,<br /> to \n
* Input: contents = contents to replace
* Example: {$text|br2nl}
* -------------------------------------------------------------
*/
function smarty_modifier_br2nl($string)
{
$string=str_ireplace("<br />","\n",$string);
return(str_ireplace("<br>","\n",$string));
}

/* vim: set expandtab: */

?>
[/php:1:c3bb6390f0]

Whoops, just noticed that the str_ireplace function is not yet part of the stable PHP, its only in CVS, more compatible code, but less flexible.

[php:1:c3bb6390f0]<?php

/*
* Smarty plugin
* -------------------------------------------------------------
* Type: modifier
* Name: br2nl
* Version: 1.0
* Date: May 13, 2003
* Author: Michael Wulff Nielsen
* Based on: nl2br by Monte Ohrt <monte@ispi.net>
* Purpose: convert <br>,<br /> to \n
* Input: contents = contents to replace
* Example: {$text|br2nl}
* -------------------------------------------------------------
*/
function smarty_modifier_br2nl($string)
{
$string=str_replace("<br />","\n",$string);
return(str_replace("<br>","\n",$string));
}

/* vim: set expandtab: */

?>
[/php:1:c3bb6390f0]

Have fun with it.
Michael Wulff Nielsen
Back to top
View user's profile Send private message
andre
Smarty Pro


Joined: 23 Apr 2003
Posts: 164
Location: Karlsruhe, Germany

PostPosted: Tue May 13, 2003 10:31 am    Post subject: Reply with quote

I would use a preg replace like this:
[php:1:ce602e3b8b]$string=preg_replace('/\<br\s*\/?\>/i', "\n", $string);[/php:1:ce602e3b8b]

this will replace everything of the following:
[php:1:ce602e3b8b]$string = "this is a test of <br/> and <br> and <br > and <br /> and <BR> and <Br />";[/php:1:ce602e3b8b]
Back to top
View user's profile Send private message
naish
Smarty n00b


Joined: 13 May 2003
Posts: 4

PostPosted: Tue May 13, 2003 11:23 am    Post subject: Reply with quote

Well, I personally don't know regexp's. But something tells me that I should learn them fast. I will insert your expression into my code here.

BTW. How does one go about submitting code for inclusion into smarty?

Kind regards
Michael Wulff Nielsen
Back to top
View user's profile Send private message
AZTEK
Smarty Pro


Joined: 16 Apr 2003
Posts: 235
Location: Purdue University

PostPosted: Tue May 13, 2003 2:54 pm    Post subject: Reply with quote

Probably email it into the mailing list
_________________
"Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue May 13, 2003 4:49 pm    Post subject: Reply with quote

If you're interested in regex's, there is a book recommendation near the end of this thread http://www.phpinsider.com/smarty-forum/viewtopic.php?t=243 as well as some indication as to what you are getting yourself into Wink The book, is awesome!
Back to top
View user's profile Send private message
Justin
Smarty Regular


Joined: 07 May 2003
Posts: 38
Location: Vilnius, Lithuania

PostPosted: Tue May 13, 2003 5:27 pm    Post subject: Reply with quote

andre wrote:
I would use a preg replace like this:
[php:1:44b2fa21bb]$string=preg_replace('/\<br\s*\/?\>/i', "\n", $string);[/php:1:44b2fa21bb]


It's very simple regexp ant I think, that str_replace will work faster than preg_replace..
_________________
http://www.baubas.net
Back to top
View user's profile Send private message Visit poster's website
aloner
Smarty Rookie


Joined: 24 Apr 2003
Posts: 24

PostPosted: Tue May 13, 2003 5:38 pm    Post subject: Reply with quote

str_replace() will not convert tags like <br /> (additional spaces), which are valid HTML for browser. So the only way to do it _correctly_ is to use regexes.
_________________
Your ad here.
Back to top
View user's profile Send private message
sweatje
Smarty Regular


Joined: 17 Apr 2003
Posts: 70
Location: Bettendorf, Iowa, USA

PostPosted: Tue May 13, 2003 7:28 pm    Post subject: Reply with quote

regexp should even pick up
Code:
<br class="funky_break_type" />
Surprised
_________________
Jason
jsweat_php AT yahoo DOT 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 -> Plugins 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