Re: [SMARTY] nl2br in a template?
| From: | Monte Ohrt | Date: | Wed, 26 Feb 2003 22:13:53 +0000 |
| Subject: | Re: [SMARTY] nl2br in a template? | ||
| References: | 1 2 3 4 5 | Groups: | php.smarty.dev php.smarty.general |
| Request: | Send a blank email to php-smarty-dev+get-849@lists.php.net to get a copy of this message | ||
OK if we don't worry about preceeding <br> tags, then a wrapper to PHP
nl2br() should suffice... Otherwise I have it written:
function smarty_modifier_nl2br($string, $preceed_test = true)
{
if(!$preceed_test) {
return preg_replace('%(?:\r\n|\r|\n)%m', '<br />', $string);
} else {
return preg_replace('%(?:\<br(?:\s*\/)?\>)?(?:\r\n|\r|\n)%', '<br
/>', $string);
}
}
Monte
On Wed, 2003-02-26 at 16:00, 1LT John W. Holmes wrote:
> > This can be added, sounds useful enough. A few questions:
> >
> > 1) Is there a better name, or keep nl2br for consistancy of PHP
> > developers familiarity? nl2br is nice and short though...
>
> Keep it.
>
> > 2) Should it always return XHTML compliant code <br />, regardless of
> > PHP version? the PHP command nl2br() pre-PHP 4.0.5 doesn't output XHTML
> > break tags. I think it would be good to always be XHTML compliant.
>
> So you're going to re-implement the function and not use the default
> nl2br()? If so, then yeah, always make it output XHTML compliant tags.
>
> > 3) Should it cover newline formats from Mac/PC appropriately? In
> > otherwords replace \n or \r\n with <br /> tags?
>
> The regular nl2br() does this already, so yeah, it should.
>
> > 4) Should it replace newlines already preceeded by a <br> tag?
> > Controllable option? If so, what should be default?
>
> Yes, replace any newlines. If your text already has HTML code in it, then
> you probably shouldn't be calling nl2br() on it anyhow.
>
> Overall, just a wrapper to the regular nl2br() function would be the best, I
> think. Why recreate it?
>
> Wouldn't it just be smarter overall to convince people to use the
> $smarty->register() method? (which I just learned about) Otherwise it seems
> like you'd just end up writing a smarty.modifier file for every PHP function
> that people want.
>
> ---John Holmes...
--
Monte Ohrt <monte@ispi.net>