Link to home
Start Free TrialLog in
Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of Rob
Rob
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bruce Gust

ASKER

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!