-1

I am looping through a list of Discord guild names in node.js to generate a php file containing an array in the following format

<?php

$guildLookup = array( "164930842483761" => "guildName1",
"56334196291325" => "guildName2",
);

?>

One of the guild names is below

/ text \

so when I insert it in the php array it looks like

<?php

$guildLookup = array( "164930842483761" => "guildName1",
"56334196291325" => "guildName2",
"56334196291342" => "/     text    \",
"56334196291135" => "guildName4",
);

?>

which generates the following error when trying to access php array.

PHP Parse error: syntax error, unexpected '487648187306475542' (T_LNUMBER), expecting ')'

presumedly since the backslash escapes the " at end of the line.

I tried a couple of variations on the following in node.js to escape a backslash at end of a guild name or even getting rid of it before putting it in php file but can't seem to get it working. Appreciate any suggestions I can try.

var newString = orginalString.replace(/\\$/, '\\');
MorningSleeper
  • 415
  • 1
  • 5
  • 13

1 Answers1

0

'\\' means \, you must replace with 4 \\\\, that's two escaped \

//Use 4 backslashes for \\
//Also remove the $ because escaped characters can be anywhere
var newString = orginalString.replace(/\\/, '\\\\');

Also, I suggest you validate for quotes.

var newString = orginalString.replace(/'/, "\\'");
Mr. bug
  • 366
  • 2
  • 11