0

I'm using Ubuntu 16.04LTS, HTML, PHP

I make C/C++/java/Python code marking web Site.

I submit this code in HTML

#include <stdio.h>

int main()
{
  int a,b;
  scanf("%d %d",&a,&b);
  printf("%d + %d = %d",a,b,a+b);
  return 0;
}

this text in

<input type="hidden" name="code" id="code">

and use POST method go to PHP

$code = str_ireplace("\x0D", "\n", $_POST['code']);

but server open this code

#include <stdio.h>  int main() { int a,b; scanf("%d %d",&a,&b); printf("%d + %d = %d",a,b,a+b); return 0; }

so I use ubuntu command "sed"

sed -e "s/^M/\n/g" $file > $newfile

but this result

#include <stdio.h>

 int main()
 {
 int a,b;
 scanf("%d %d",&a,&b);
 printf("%d + %d = %d",a,b,a+b);
 return 0;
 }

remove tab and add line first char space

How can I solve this problem?

Jan Sršeň
  • 1,045
  • 3
  • 23
  • 46
  • What is the problem here? All 3 versions of the *C* code you posted should be compilable (as *C* compilers don't care about indents, *eoln*s and so on). If this is simply about text processing in *html* you should update the question tags (remove irrelevant ones). – CristiFati Sep 15 '18 at 09:00
  • @CristiFati The second version should not compile. You are not supposed to write code after the preprocessor commands (such as #include). – vdavid Sep 15 '18 at 10:53
  • I don’t think you should try to get back the whitespaces you lost. You should make sure that you did not lose them in the first place. I suppose you lose them during the POST command. This may help: https://stackoverflow.com/questions/6344708/how-to-echo-an-input-of-an-textarea-with-line-breaks – vdavid Sep 15 '18 at 11:00
  • @vdavid: You're right, I missed that aspect the 1st time. – CristiFati Sep 15 '18 at 14:08
  • newline characters are different on Windows vs everyone else. – stark Sep 15 '18 at 23:21

0 Answers0