php argument return a string with newline -


i have php script takes in 1 argument e.g. , need capture argument , urlencode

here sample code show issue

<?php  $input=$argv[1]; #$input="how you.\nare free?"; echo "$input\n"; $escape_input = urlencode("$input"); echo "$escape_input";  ?> 

$ php test.php "how you.\nare free?" 

the output how+are+you.%5cnare+you+free%3f% incorrect new line not encoded properly.

but if hardcode same string in code, work correct output: how+are+you.%0aare+you+free%3f%

the problem input of code. have pointed out

echo urlencode("hello\nworld."); 

prints hello%0aworld., because \n interpreted new line character if used inside quotes in php, , \n replaced new line character before passed urlencode.

when execute code php test.php "how you.\nare free?" (lets in bash), $argv[1] string contains backslash character followed n. urlencode encodes backslash , leaves n is.

in order run code new line character, should hit enter line break should be.

$ php test.php "how you. free?" 

alternatively can manually interpret sequence \n in input , replace new line character.

$input = str_replace("\\n", "\n", $argv[1]); 

please note first argument, search pattern, string \n (backslash followed n), , second argument, replace string, new line character.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -