Need Returns in HTML Source

I am building a function that wraps <p></p> around each paragraph entered into a TextArea.

It is pretty close to working, except that when I do View—> Source, everything is on one super long line?!

How do I put carriage returns or whatever they are called in the Source?

Here is my code…


function nl2p($string, $line_breaks = true) {
	// Remove existing HTML formatting to avoid double tags.
	$string = str_replace(array('<p>', '</p>', '<br>', '<br/>'), '', $string);

	// Replace Carriage Return with Empty String.
	// Replace multiple Newlines with closing & opening paragraph tags.
	// Replace single Newline with break tag.
	if ($line_breaks == true) {
		return '<p>'.preg_replace(array("#\\r#","#\
{2,}#", "#\
#"), array("","</p><p>", "<br />"), $string).'</p>';
	}else{
		return '<p>'.preg_replace("/\
/", "</p>\
<p>", trim($string)).'</p>';
	}
}

echo nl2p($text, TRUE);

If I add
into the line below with single quotes, then it just adds a visible
to the source…

	return '&lt;p&gt;'.preg_replace(array("#\\r#","#\

{2,}#", “#
#”), array(‘’,'</p><p>
', ‘<br />
‘), $string).’</p>’;

And if I add
into the line with double quotes, then it ends up adding extra carriage returns in my output…

	return '&lt;p&gt;'.preg_replace(array("#\\r#","#\

{2,}#“, “#
#”), array(”“,”</p><p>
", "<br />
"), $string).‘</p>’;

I think my preg_replace is the culprit…

Debbie

NVM

Huh???

Debbie

Why put carriage returns in the code any ways? What doe it matter if it is all on one line? All the debugging tools I’ve used concerning HTML format the HTML into a proper tree, Firebug one such example.

You are right in that I am being anal-retentive here, but why not shoot for the stars? :lol:

Just to clarify, here is what I see now…

And this is how it should normally look…

So can that minor issue be fixed?

Thanks,

Debbie

One of the differences between ’ ’ and " " in PHP is which escape characters they recognise. Between ’ ’ the only escape character recognised is \’

To have the
escape recognised you must wrap it in " ".

But when I do that in my code it is also inserting carriage returns in the output?! :-/

Here is a function I wrote that seems to work pretty well, but View—>Source yields everything on one line which is really ugly and a pain.

(I guess viewing the source in a “DOM Tree” in some editors would fix that, but I am old-school and want it working in the browser’s source as well.)

Here is my function…


$text="I decided to start my own business because I want to be my own boss!



My boss is a jerk and never appreciates anything that I do for him, so why put up with the abuse?!  He takes me for granted and doesn't appreciate all of my talents.

Running my own business will give me a chance to do things as I see fit...

Line One
Line Two
Line Three
Line Four



Line Eight

Line Ten";


$text2 = htmlentities($text, ENT_QUOTES);


function nl2p($string, $line_breaks = true) {
	// Remove existing HTML formatting to avoid double tags.
	//^	$string = str_replace(array('<p>', '</p>', '<br>', '<br/>'), '', $string);

	// Replace Carriage Return with Empty String.
	// Replace multiple Newlines with closing & opening paragraph tags.
	// Replace single Newline with break tag.
	if ($line_breaks == true) {
		return '<p>'.preg_replace(array("#\\r#", "#\
{3,}#", "#\
{2}#", "#\
#"), array("", "</p><br /><p>", "</p><p>", "<br />\
"), $string).'</p>';
//		return '<p>'.preg_replace(array("#\\r#","#\
{2,}#", "#\
#"), array("","</p><p>", "<br />\
"), $string).'</p>';
	}else{
		return '<p>'.preg_replace("#\
#", "</p><p>", trim($string)).'</p>';
	}
}

echo nl2p($text2, TRUE);

If you can help me figure out why I get the View—>Source I do, I’d be grateful!!

Thanks,

Debbie

Do you mean you want line breaks between the paragraph tags?

return ‘<p>’.preg_replace(array(“#\r#”, “#
{3,}#”, “#
{2}#”, “#
#”), array(“”, “</p>
<br />
<p>”, “</p>
<p>”, "<br />
"), $string).‘</p>’;

Do I want each <p> on a separate line in my View–> Source, yes!!

But do I want those to show up as additional carriage returns in my output, no!! (Unfortunately they do.)

Try running my code above - with your “fix” - and you will see what I mean…

Debbie

I ran your code, but I don’t see what you mean. :-/

You may need to post what you see when you view source.

First of all, I am on a MacBook…

Here is the output of my original code…

And here is the one mega long line of HTML when I do View—>Source…

After I add in
into the code like you suggested…

if ($line_breaks == true) {
	return '&lt;p&gt;'.preg_replace(array("#\\r#", "#\

{3,}#“, “#
{2}#”, “#
#”), array(”", “</p>
<br />
<p>”, “</p>
<p>”, "<br />
"), $string).‘</p>’;

…then here is how the output changes… (Notice how there are extra carriage returns now.)

Normally adding a
in between double quotes would just affect how the HTML is formatted when you View—>Source, but here my Regex is treating it like it is to be output.

Hope you see what I am saying now. (And remember that I am on a Mac…)

Thanks,

Debbie

What a waste time of time… If you were working on a paid project your boss would probably slap you (maybe not literally) but yeah. This needs to be done like you need a hole in your head. In fact you page will use up more bandwidth by adding extra characters. I know how much everyone in HTML and CSS forum complains about squeezing every byte of performance out of page so why should this be any different. So long as the server-side code is readable HTML source formatting doesn’t matter. Just saying… though if it makes you happy and your not investing any client money it than have fun… I guess.

+1

Ahh, it’s because of the multiple replacements. The third replace changes
{2} into </p><p>. But if you put a newline in there, <p>
</p>, then the fourth replacement changes that newline into a <br>. Trying to stagger the replacements to get the right output is going to be tricky. It might be easier with a callback…

// warning: untested, and requires >= PHP 5.3

$string = preg_replace('/\\r/', '', $string);
$string = preg_replace_callback('/\
+/', function ($matches) {
    if (strlen($matches[0]) >= 3) {
        return "</p>\
<br />\
<p>";
    } elseif (strlen($matches[0]) >= 2) {
        return "</p>\
<p>";
    } else {
        return "<br />\
";
    }
}, $string)

Though, it would probably be a lot easier just to use the standard nl2br. I’m pretty sure screen readers and search engines will still read it just the same, and it’ll make your life a lot easier.

If you care so much about the HTML’s output formatting…
http://us3.php.net/tidy


<?php ob_start(); # This is important...

// Your code goes here...

$super_useless_output = ob_get_clean();

$tidy_options = array( # See http://tidy.sourceforge.net/docs/quickref.html
  'clean' => true,
  'char-encoding' => 'utf8',
  'output-html' => true,
  'doctype' => 'strict',
  'bare' => true,
  'fix-uri' => true,
  'indent' => true,
  'indent-spaces' => 4,
  'tab-size' => 4,
  'wrap' => 0,
);

$tidy = new Tidy();
$tidy->parseString( $super_useless_out, $tidy_options, 'utf8' );
$tidy->cleanRepair();

echo $tidy; ?>