View previous topic :: View next topic |
Author |
Message |
naish Smarty n00b
Joined: 13 May 2003 Posts: 4
|
Posted: Tue May 13, 2003 9:26 am Post subject: A br2nl modifier. |
|
|
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 |
|
andre Smarty Pro
Joined: 23 Apr 2003 Posts: 164 Location: Karlsruhe, Germany
|
Posted: Tue May 13, 2003 10:31 am Post subject: |
|
|
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 |
|
naish Smarty n00b
Joined: 13 May 2003 Posts: 4
|
Posted: Tue May 13, 2003 11:23 am Post subject: |
|
|
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 |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Tue May 13, 2003 2:54 pm Post subject: |
|
|
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 |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
|
Back to top |
|
Justin Smarty Regular
Joined: 07 May 2003 Posts: 38 Location: Vilnius, Lithuania
|
Posted: Tue May 13, 2003 5:27 pm Post subject: |
|
|
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 |
|
aloner Smarty Rookie
Joined: 24 Apr 2003 Posts: 24
|
Posted: Tue May 13, 2003 5:38 pm Post subject: |
|
|
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 |
|
sweatje Smarty Regular
Joined: 17 Apr 2003 Posts: 70 Location: Bettendorf, Iowa, USA
|
Posted: Tue May 13, 2003 7:28 pm Post subject: |
|
|
regexp should even pick up
Code: | <br class="funky_break_type" /> | _________________ Jason
jsweat_php AT yahoo DOT com |
|
Back to top |
|
|