Array elements have extra character

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
jimandy
New php-forum User
New php-forum User
Posts: 10
Joined: Sun May 15, 2011 5:23 pm

Sun Jul 21, 2019 1:02 pm

The code below is a highly abstracted example of my puzzlement. In this skeletal code the text area is populated but user would actually be pasting more realistic info into it.

There are two lines of code as options to make an array with submitted data. Commenting out the line that hard-codes $listArray results in the display of
x: a b
(note the space between which is not desired in my project.
What is desired is
y: ab
(no space in between the "a" and the "b") which works if you comment out the line that explodes $dataIn
It seems that exploding using \n is leaving some character in the elements of the array.

Code: Select all

<?
If (isset($_POST['dataIn'])) {
$list= $_POST['dataIn'];
$listArray = explode("\n", $list); print("x: ");
//$listArray  = array("a","b");    print("y: ");
$nbrInList = count($listArray);
for ($i = 0; $i <=$nbrInList-1; $i++) {echo ($listArray[$i]);}
}
?>

<html> 
<form method="POST">	               
<textarea  name="dataIn" rows="5" cols="10">
a
b
</textarea>                
<input type="submit" value="submit list">            
</form>
</html>
jimandy
New php-forum User
New php-forum User
Posts: 10
Joined: Sun May 15, 2011 5:23 pm

Sun Jul 21, 2019 1:42 pm

I think I just solved it. Added
trim($listArray[$i],"\r");
which seems to do the trick. Guess my OS was sending both newline AND a carriage return.
Post Reply