When converting new line characters to HTML break (<br>
)
What is Cleaner to use?
str_replace(array("\r\n", "[br]", "\r"), '<br>', $Content);
OR
str_replace("\r\n", '<br>', $Content);
str_replace("[br]", '<br>', $Content);
str_replace("\r", '<br>', $Content);
I've been using the first method, but a friend of mine said that the way I currently use is more sloppy in terms of processing.
I'm using the formatting in a function; so
function Formatting ($Content)
{
$Content = str_replace(array("\r\n", "[br]", "\r"), '<br>', $Content);
return $Content;
}
I would tweak this one myself (from the user notes in the manual page http://php.net/manual/en/function.nl2br.php:
<?php
/**
* Converts newlines and break tags to an
* arbitrary string selected by the user,
* defaults to PHP_EOL.
*
* In the case where a break tag is followed by
* any amount of whitespace, including \r and \n,
* the tag and the whitespace will be converted
* to a single instance of the line break argument.
*
* @author Matthew Kastor
* @param string $string
* string in which newlines and break tags will be replaced
* @param string $line_break
* replacement string for newlines and break tags
* @return string
* Returns the original string with newlines and break tags
* converted
*/
function convert_line_breaks($string, $line_break=PHP_EOL) {
$patterns = array(
"/(<br>|<br \/>|<br\/>)\s*/i",
"/(\r\n|\r|\n)/"
);
$replacements = array(
PHP_EOL,
$line_break
);
$string = preg_replace($patterns, $replacements, $string);
return $string;
}
?>