I have the following line of code in php that I m using to execute the python screen. and it takes in text from a html textarea which will contain newline characters
$string = $_POST['textarea']; // e.g. "String 1\n string 2\n"
$command = escapeshellcmd("python script.py -c \"$string\"");
I have managed to find a way to do this.
In the php file, replace all the newline with '\n' before calling escapeshellcmd
$string = trim(preg_replace('/[\r\n]+/', '\n', $_POST['textarea'])); // For a string like: // abcd // defg // will become // abde\\ndefg
Since the newline char '\n' has been escaped by php escapeshellcmd with the prepended slash. the shell will be able to process the string and python will receive abde\ndefg as one of the arguments. Next in the python code, we simply have to undo the escape using the code below
arg = sys.argv.decode('string_escape')
I hope the above solution will be able to help those who are facing the similar issue as me. =)