Hi,
I want to take php string in javascript variable.
How to take it?
php string includes \n so while taking it in variable it shows me error of "SyntaxError: unterminated string literal"
I got one solution form stackoverflow that use json_encode
but after use of json_encode is giving me string by adding extra \n
for example
my string is

I have done many applications in this skill.

Lastest work is done in GS Lab.
So sending this for manager approval.

but on use of json_encode it is giving me

    I have done many applications in this skill.



    Lastest work is done in GS Lab.

    So sending this for manager approval

code is as below:

var _previous_description='<?php echo json_encode($previous_description); ?>';
var desc = _previous_description.substring(1, _previous_description.length-1); // it is to remove extra "" added while encoding at the start and end of string
alert(desc); // it gives me strinf with extra spacces.
// current values
var c_description=$('#previous_description').val();
alert(c_description); // it gives correct value

What is wrong here?
how to remove this extra lines?
or is there any simple solution for this?

var _previous_description='<?php echo json_encode($previous_description); ?>';
var desc = _previous_description.substring(1, _previous_description.length-1);
desc=desc.replace(/(\r\n|\n|\r)/gm,"\n");

This worked for me.
Kindly let me know in any case it fails.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.