How can I replace my line breaks?

I've got a piece of Javascript code that shuts down (understandably) when I've got line breaks in my text. Take a look:

function Family() {
var c_description = new Array();

c_description[7699]="For this project type to obtain a waiver of due diligence all 4 of the following requirements must be met:\n(1) The Landlord offers standard environmental contractual protections in the lease\n(2) The building is of recent construction post-1980\n(3) The project DOES NOT require roofing material disturbance or core drilling\n(4) The project DOES NOT require flooring material disturbance. Everybody says \"No\"";

y = document.getElementById("compliance_id");
compliance_id = y.options[y.selectedIndex].value;
document.getElementById("c_description").value=c_description[compliance_id];
document.getElementById("f_address_one").value=f_address_one[compliance_id];
document.getElementById("f_address_two").value=f_address_two[compliance_id];
document.getElementById("f_city").value=f_city[compliance_id];
document.getElementById("f_zip").value=f_zip[compliance_id];
document.getElementById("f_state").value=f_state[compliance_id];
document.getElementById("f_phone").value=f_phone[compliance_id];
document.getElementById("f_cell").value=f_cell[compliance_id];
document.getElementById("f_email").value=f_email[compliance_id];
document.getElementById("f_alt_email").value=f_alt_email[compliance_id];
document.getElementById("f_home_church").value=f_home_church[compliance_id];
document.getElementById("f_subdivision").value=f_subdivision[compliance_id];
}

Open in new window


What's happening is that I've got a select menu that "onchange" populates several fields based on the id that was selected:

<select name="compliance_id" id="compliance_id" onchange="Family();">
<option><option>
<option value="7699">Adams, 1412 Savannah Park Dr</option>
</select>

In this instance, when "Adams" is selected, the corresponding info is placed into a textarea and it looks great AS LONG AS MY LINE BREAKS have been accurately accounted for. You can see what I'm talking about on line 4 in the above code box: As long as "(1) is documented "\n(1)," I've got an accurate line break as well as functioning code.

My challenge is to retrieve the data from a MySQL database and ensure that all line breaks are documented with the "\n" character. To do that I'm using this function:

	public function list_compliance_code($compliance_list) {
		
		$briefcase="";
		
		foreach($compliance_list as $compliance)
		{
			$stunt_description=str_replace('""', '\"', $compliance['description']);
			$the_description=str_replace("\r\n", "\n", $stunt_description);
			$stunt_requirements=str_replace('""', '\"', $compliance['requirements']);
			$the_requirements=str_replace("\r\n", "\n", $stunt_requirements);
			$the_determination=str_replace('""', '\"', $compliance['determination']);
			$briefcase .="c_description[";
			$briefcase.=$compliance['id'];
			$briefcase.="]=\"";
			$briefcase.= $the_description;					
			$briefcase.="\";c_requirements[";
			$briefcase.=$compliance['id'];
			$briefcase.="]=\"";
			$briefcase.= $the_requirements;	
			$briefcase.="\";c_determination[";
			$briefcase.=$compliance['id'];
			$briefcase.="]=\"";
			$briefcase.=$the_determination;
			$briefcase.="\";";
		}
		
		return $briefcase;
		
	}

Open in new window


On line 8, I'm attempting to replace any line breaks with the "\n" character and it isn't working. When I go to check the source code of the page, instead of seeing this:

c_requirements[2]="For this project type to obtain a waiver of due diligence all 4 of the following requirements must be met:\n(1) The Landlord offers standard environmental contractual protections in the lease\n(2) The building is of recent construction post-1980\n(3) The project DOES NOT require roofing material disturbance or core drilling\n(4) The project DOES NOT require flooring material disturbance\n\n If you answer \"No\" to any of the 4 requirements, you must select \"No\" in the drop down box. ";

I'm seeing this:

c_requirements[2]="For this project type to obtain a waiver of due diligence all 4 of the following requirements must be met:

(1) The Landlord offers standard environmental contractual protections in the lease
(2) The building is of recent construction post-1980
(3) The project DOES NOT require roofing material disturbance or core drilling
(4) The project DOES NOT require flooring material disturbance

 If you answer \"No\" to any of the 4 requirements, you must select \"No\" in the drop down box. ";

What do I need to do differently?
brucegustPHP DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
try also to replace the \r on its own.  Different encodings will use ascii 10 (\r) ascii 13 (\n) or a combination of both.

e.g.
$the_description=str_replace("\r", "\n", $stunt_description);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark BradyPrincipal Data EngineerCommented:
I would use the nl2br() function which will turn all newline characters into html <br>
Works great.

$your_data = nl2br($data);
0
Ray PaseurCommented:
If you're using consistent character-set encoding across all of the application, your end-of-line characters should be completely benign and should work correctly* in PHP, JavaScript, HTML, XML, JSON, and MySQL.  In other words, the EOL character is no different from any other character and requires no special handling.   Today the character set of choice is UTF-8.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11880-Unicode-PHP-and-Character-Collisions.html

PHP has a built-in, context-aware end-of-line character constant, PHP_EOL.  It's wise to use this in your server scripts instead of messing around with \r, \n, or combinations of line-feed, etc.  PHP trim() will recognize EOL characters as whitespace, and remove unwanted EOL characters from the ends of a string.  Regular expression replacement of whitespace will remove these characters from embedded positions in a string.  There may be cases where you want to do that, but I find it's pretty rare in practice.  If you decide there is a real need to change the user input, the top notes and linked functions here are pretty useful.

E-E had another question recently about new line characters.
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28677866.html#a40790428

* This is what "correctly" means:
By the standards, browsers display textual data under a rule that requires them to collapse all instances of whitespace + multiple whitespace characters into a single blank.  Six blanks in a row in the original will be rendered as a single blank.  Twenty line-feeds will be rendered as a single blank.  Two tabs, a blank, and three EOL characters will be rendered as a single blank.  To preserve original whitespace formatting, you can use the <pre> tag.
0
brucegustPHP DeveloperAuthor Commented:
Gentlemen! Thanks, as always for your time and expertise.

I got things to work by hardcoding the needed Javascript directly on the page, so it looked like this:

c_description[22]="Determine contract obligations for performance of environmental due diligence for the project prior to construction activities.  If vendor is operating in \"Contractor\" capacity to Verizon (e.g. Crown), ensure project proposals clearly state the party with responsibility for environmental and worker safety inspections and any applicable AHJ regulatory notifications for building materials renovations.";
c_requirements[22]="Both of the following requirements must be met:\n\n(1) This is a \"turn key\" project where vendor is operating in \"Contractor\" capacity to Verizon.\n(2) Project proposals clearly state the party with responsibility for environmental worker safety inspections and  any applicable authority having jurisdiction regulatory notifications for building materials renovations. If you answer \"No\" to  either of the requirements, you must select \"No\" in the drop down box.";
c_determination[22]="Contact your Area Compliance for further guidance on your submission.";

Open in new window


Getting everything to print on one line with the needed "\n\n" for my line breaks in order to accommodate the particulars of Javascript proved to be different than merely replacing characters using nl2br or str_replace. I found one "json_encode" which got me close, but still no cigar. So, in order to honor a deadline, I just went with hard coding thing and it worked.

Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.