Get rid of multiline PHP variables

I tried out a form to collect sentences. Works well, BUT: 1 student pressed enter in the textarea number 18
This pushed all his answers down 1 line and his score was very bad.

<textarea cols="26" rows="1" maxlength="26" placeholder="Write your answer here." name="G18"></textarea>

Apparently, setting rows=“1” does not prevent the use of ‘enter’ in a textarea like I hoped.

I’m wondering if it is possible to look at each php variable before they get sent and get rid of new line characters?

This code, kindly suggested to me here puts an X in empty variables

if($studentnr == '') {$studentnr = 'X';}

for ($i=1; $i <= 35; $i++) {

    if (${"q$i"} == '') ${"q$i"} = 'X'; 

Maybe something similar can get rid of newline characters? Thanks for any tips or links!

Edit: I tried this, it seems to work. Can it be tidied up a bit?

for ($i=1; $i <= 35; $i++) {
	$str = ${"q$i"};
	$order   = array("\r\n", "\n", "\r");
    $nonewlines = str_replace($order, '', $str);
    ${"q$i"} = $nonewlines;

Why? Just use trim() and strtr() before using the variable. And use arrays, it’s much cleaner then your variable juggling.

1 Like

How would I do that?

Until the student writes something, I don’t have a variable.

You mean before the student clicks ‘send’ do something?


$texts = [
    " is a test ",
    "\n with newline",
    "\n everywhere \n",
    "\r and carriage return \r\r\r",
    "\r and \n some \r\n mixed \n \r stuff \r\n\n\r",

$normalized = array_map(function($text){
    $trimed = trim($text);
    $replaced = strtr($trimed, ["\n" => '', "\r" => '']);
    return $replaced;
}, $texts);


array(6) {
  string(4) "this"
  string(9) "is a test"
  string(12) "with newline"
  string(10) "everywhere"
  string(19) "and carriage return"
  string(24) "and  some  mixed   stuff"

Please forgive my obtuseness, but I really know nothing about php. I’m not sure where I should put the code you suggest, but I’d like to try it.

My <form> has this:

<form method="post" action="php/thankyou18BEweek17.php" enctype="text/plain">

So when they press send, it invokes thankyou18BEweek17.php

Your code should go at the top of my thankyou.php, before the variables are assigned??

// your code here??

$studentnr = $_POST['sn'];
$q1 = $_POST['1'];
$q2 = $_POST['2'];
$q3 = $_POST['3'];
$q4 = $_POST['4'];

You can use the code whenever you think you need it, it just strips out any linebreaks. But you have to provide an array with all the texts.

1 Like

Perhaps JavaScript would be an easier solution for your scenario?

This script could be included at the bottom of the same page that you have your textareas in. It adds an event listener to all of the textarea elements on the page. If one of the listeners detects an ‘enter’ key press, it blocks the default behavior and pops up an alert.

	var listOfTextareas = document.getElementsByTagName("textarea");

	function applyListenerToTextarea(textareaElement)
		textareaElement.addEventListener('keypress', function (e) {
				if (e.keyCode == 13 && !e.shiftKey)
					// prevent default behavior
					alert("The 'enter' key is not allowed.");
					return false;

Example of working code:

1 Like

Great, I will definitely try that! Thanks a lot!

1 Like

Just got round to trying it! Works like a dream!

Thank you very much!

I managed to bend some php to get rid of the newline characters, but “Prevention is better than cure!”

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service