Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coping with line breaks in MySQL MEMO field

Status
Not open for further replies.

rhyno2k

IS-IT--Management
Jun 9, 2001
222
US
Hi,


I'm having a helluva time dealing with getting data from a MySQL MEMO field, using PHP, into a JavaScript array.

The primary problem I'm having is an unterminated string literal. It's caused by the array declaration line spilling over to multiple lines. This is happening because some of the MySQL data I'm pulling in contains line breaks that aren't caught by my str_replace("\n"," ",$text) function.

This data appears as:
Code:
1024 Widget
1028 Gadget
in Navicat (MySQL GUI)

As such, this invisible line break makes its way into the javascript line and breaks my script.

e.g.
var parts = new Array('...','...','...
...');

How can I get around this? I've been googling & spinning my wheels for hours.


Thanks,
--RHYNO
 
What does your [tt]str_replace[/tt] function look like?

I'd be going with a regular expression:
Code:
var regEx = new RegExp( "\\n", "g" );
var strStripped = strOriginal.replace(regEx, "");

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Webflo
 
How can I get around this?

This is likely a better question for whatever server side language you're using. It may be causing problems with your client-side script, but the root of the problem exists on the server.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
dwarfthrower,

My str_replace is:
Code:
$text = str_replace("\n"," ",$text);

My JS (using PHP to populate Array decl. string):
Code:
	var parts = new Array(
	<?php
		$str = '';
		foreach($parts as $p) {
				$text = stripslashes( trim($p['partsused']) );
				$text = str_replace("\n"," ",$text);
				$str .= '"' . $text . '",';
			}
			$str = substr($str,0,-1);
			echo $str;
		?>);

($parts is a multi-dimensional PHP array)

Kaht, I think some PHP function-foo might be needed here...
 
Dwarf, I did wrangle it through PHP. Just needed to add another replace for "\r" (carriage return).

Code:
$text = str_replace("\n"," ",$text);
$text = str_replace("\r"," ",$text);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top