Convert line breaks to BR tags
Submitted by cmer on Wed, 03/05/2008 - 18:35.
I was annoyed by the fact that all the line breaks I added in tasks were not properly displayed in the browser.
To solve the problem, I added one line to the clean function in environment/functions/general.php.
My clean function now looks like this:
function clean($str) {
$str = preg_replace('/&(?!#[0-9]+;)/s', '&', $str);
$str = str_replace(array('<', '>', '"'), array('<', '>', '"'), $str);
$str = nl2br($str); // I added this line
return $str;
} // cleanI think this modification should be part of the next release. I don’t see why anyone would -not- want this feature.
Thoughts?

I worked on my hack a little bit. If you’re interested, ignore my previous message and do this instead:
in environment/functions/general.php, replace the clean function with:
/*** Equivalent to htmlspecialchars(), but allows &#[0-9]+ (for unicode)
*
* This function was taken from punBB codebase <http://www.punbb.org/>
*
* @param string $str
* @return string
*/
function clean($str) {
$str = preg_replace('/&(?!#[0-9]+;)/s', '&', $str);
$str = str_replace(array('<', '>', '"'), array('<', '>', '"'), $str);
// Replace line breaks with <br />. I don't usr nl2br because it doesn't remove the line breaks, it just
// adds the <br />.
$str = str_replace("\r", "", $str); // Remove \r
$str = str_replace("\n", "<br />", $str); // Replace \n with <br />
return $str;
} // clean
/**
* This function cleans up a string and make it ready to be displayed in a textarea field.
* Most notably, it replaces <br /> with line breaks which is easier to read for the user.
*
* @param string $str
* @return string
*/
function clean_for_textarea($str) {
$str = clean($str);
$str = str_replace("<br />", "\n", $str);
return $str;
}
And in applications/helpers/form.php, replace the textarea_field function with:
/**
* Return textarea tag
*
* @access public
* @param string $name
* @param string $value
* @param array $attributes Array of additional attributes
* @return string
*/
function textarea_field($name, $value, $attributes = null) {
if (!is_array($attributes)) {
$attributes = array();
} // if
$attributes['name'] = $name;
if (!isset($attributes['rows']) || trim($attributes['rows'] == '')) {
$attributes['rows'] = '10'; // required attribute
} // if
if (!isset($attributes['cols']) || trim($attributes['cols'] == '')) {
$attributes['cols'] = '40'; // required attribute
} // if
return open_html_tag('textarea', $attributes) . clean_for_textarea($value) . close_html_tag('textarea');
} // textarea
This is a much more solid fix in my opinion.
Will this be integrated in next releases?
Be very careful here - the clean function is what sanitizes the inputs and is where the majority of security bugs are found. It will be better to find a more secure way to address this feature request.
Also note - any code changes for inclusion in the next version need to be submitted to the issue queue
I agree with you, it's boring to not have a
element.
And more, why don't have a bbcode/wiki/etc syntax to use to make ours messages more elegant ?