# Function proposal: nl2p

- Thread: https://externals.io/message/8421
- Original archive: https://news-web.php.net/php.internals/8421
- Started by Louis-Philippe Huberdeau on 2004-03-07T18:07:08+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #8421 — Louis-Philippe Huberdeau — 2004-03-07T18:07:08+00:00

URL: https://externals.io/message/8421#8421

I wrote this very simple function (modification from nl2br actually) to 
convert newlines to well formatted HTML paragraphs instead of line 
breaks, allowing better formatting of a dynamic output. It's not the 
most perfect piece of code ever written, but it works and it can be 
quite useful.

The <br /> tag does not have as much possibilities as the <p> tag when 
it comes to CSS, which gave me the idea to write this function.

--
Louis-Philippe Huberdeau


--- BEGIN ---

/* {{{ proto string nl2p(string str)
    Converts newlines to HTML paragraphs for standard compliance */
PHP_FUNCTION(nl2p)
{
	/* in brief this inserts </p>\n<p> before matched regexp \n\r?|\r\n? ,
	   <p> before the string and </p> after the string */
	zval	**zstr;
	char	*tmp, *str;
	int	new_length;
	char	*end, *target;
	int	repl_cnt = 0;
	int seen_other_char = 0;

	if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &zstr) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	
	convert_to_string_ex(zstr);

	str = Z_STRVAL_PP(zstr);
	end = str + Z_STRLEN_PP(zstr);
	
	/* it is really faster to scan twice and allocate mem once insted 
scanning once
	   and constantly reallocing */
	while (str < end) {
		if( seen_other_char ) {
			if (*str == '\r') {
				if (*(str+1) == '\n') {
					str++;
				}
				repl_cnt++;

				seen_other_char = 0;
			} else if (*str == '\n') {
				if (*(str+1) == '\r') {
					str++;
				}
				repl_cnt++;

				seen_other_char = 0;
			}
		} else if( *str != '\n' && *str != '\r' ) {
			seen_other_char = 1;
		}
		
		str++;
	}
	
	if (repl_cnt == 0) {
		RETURN_STRINGL(Z_STRVAL_PP(zstr), Z_STRLEN_PP(zstr), 1);
	}

	new_length = Z_STRLEN_PP(zstr) + repl_cnt * (sizeof("\n</p>\n<p>") - 1) 
+ sizeof("\n<p>\n") + sizeof("\n</p>");
	tmp = target = emalloc(new_length + 1);

	str = Z_STRVAL_PP(zstr);

	*target++ = '\n';
	*target++ = '<';
	*target++ = 'p';
	*target++ = '>';
	*target++ = '\n';

	seen_other_char = 0;

	while (str < end) {
		switch (*str) {
			case '\r':
			case '\n':
				if( seen_other_char ) {
					*target++ = '\n';
					*target++ = '<';
					*target++ = '/';
					*target++ = 'p';
					*target++ = '>';
					*target++ = '\n';
					*target++ = '<';
					*target++ = 'p';
					*target++ = '>';
				
					if ((*str == '\r' && *(str+1) == '\n') || (*str == '\n' && *(str+1) 
== '\r')) {
						*target++ = *str++;
					}

					seen_other_char = 0;
				}

				*target++ = *str;
				break;
			default:
				seen_other_char = 1;
				*target++ = *str;
		}
	
		str++;
	}

	*target++ = '\n';
	*target++ = '<';
	*target++ = '/';
	*target++ = 'p';
	*target++ = '>';
	*target++ = '\n';
	
	*target = '\0';

	RETURN_STRINGL(tmp, new_length, 0);
}
/* }}} */


--- END

---

## #8422 — mfischer@gjat.josefine@Markus Fischer — 2004-03-07T20:04:16+00:00

URL: https://externals.io/message/8421#8422 · In reply to: #8421

On Sun, Mar 07, 2004 at 01:07:08PM -0500, Louis-Philippe Huberdeau wrote : 
> /* {{{ proto string nl2p(string str)
>    Converts newlines to HTML paragraphs for standard compliance */

    I'm not to judge whether this goes in or not, however using
    paragraphs is not more standard compliance than breaks. Breaks
    aren't deprecated in the current version of HTML.

    It's about semantics, not about standard compliance. This should be
    called by the right name if it's used.

    - Markus

---

## #8423 — Stefan Walk — 2004-03-07T20:58:31+00:00

URL: https://externals.io/message/8421#8423 · In reply to: #8421

> I wrote this very simple function (modification from nl2br actually) to 
> convert newlines to well formatted HTML paragraphs instead of line 
> breaks, allowing better formatting of a dynamic output. It's not the 
> most perfect piece of code ever written, but it works and it can be 
> quite useful.
> 
> The <br /> tag does not have as much possibilities as the <p> tag when 
> it comes to CSS, which gave me the idea to write this function.
> 
> --

function nl2p($str) {
  return '<p>'.preg_replace('/\r\n|\n|\r/', '</p>$0<p>', $str).'</p>';
}

I think that function is not neccessary... nl2br is already redundant.
They are one-liners in php itself... so no need to write functions for
them.

-- 
Regards,
Stefan Walk
<swalk@prp.physik.tu-darmstadt.de

---

## #8425 — cyberian@arcor.de — 2004-03-07T23:35:42+00:00

URL: https://externals.io/message/8421#8425 · In reply to: #8423

> 
> > […]
> function nl2p($str) {
>   return '<p>'.preg_replace('/\r\n|\n|\r/', '</p>$0<p>', $str).'</p>';
> }
> I think that function is not neccessary... nl2br is already redundant.
> They are one-liners in php itself... so no need to write functions for
> them.

More ideas for intresting functions specialy for websites:
- nl2span($text,  $array_of_arguments_for_span)
- nl2div($text,  $array_of_arguments_for_div)
- obfuscate_mailaddr($mail_addr).

Intresting for users that need such functions on several places in code and in 
this case better as regexp in PHP itself. Why not take this ideas and put 
together into ext/html(format)?

Regards, Kai

---

## #8426 — Sara Golemon — 2004-03-08T01:32:40+00:00

URL: https://externals.io/message/8421#8426 · In reply to: #8425

> Intresting for users that need such functions on several places in code
and in
> this case better as regexp in PHP itself. Why not take this ideas and put
> together into ext/html(format)?
>
I agree with the statement that functions like these (and `nl2br()` itself)
don't really belong in the PHP core, but if you feel passionate about them,
you're more than welcome to create an htmlformat extension in PEAR or PECL.

-Sara
