0

I have a website running PHP 7.4 on ubuntu 20.04 with a form with a textarea element. Here is how it is setup.

<form id="user-input">
<textarea name="usertags">
</textarea>
</form>

When the user clicks the submit button, I am doing a POST fetch request using FormData and JSON.stringify in Javascript

formData = new FormData(document.getElementById("user-input"));
async function postUserAction(formData) {
   const response = await fetch('https://example.com/action.php', {
      method: 'POST',
      body: JSON.stringify(Object.fromEntries(formData))
   });
      return await response.json(); 

On the PHP backend

$json = file_get_contents('php://input');
$data = json_decode($json);
$tags= explode(PHP_EOL, $data->usertags);

Now, my question is if the PHP_EOL is cross platform safe regardless of what browser/system/device the user is using? I am storing the tags in a database so I need to be able to get consistent results.

novicecoder
  • 9
  • 1
  • 2

1 Answers1

0

PHP_EOL is platform dependent, meaning it corresponds to \r\n on Windows and \n on others. As pointed out here, you can try normalizing the newlines first:

$usrTags = preg_replace('~\R~u', "\n", $data->usertags);

and then do the explode:

$tags= explode("\n", $usrTags);

Since your data is coming from clientside browsers, I very much doubt you will find anything other than "\n", so you could just be fine with replacing PHP_EOL with \n instead of normalizing.

ibrahim tanyalcin
  • 5,643
  • 3
  • 16
  • 22