Jump to content

How to Remove Carriage Return in Array


soma56

Recommended Posts

I'm trying to remove blank spaces after a textarea submit. For example:

 

Blank space

Blank space

text1(enter key pressed)

text2(enter key pressed)

text3(enter key pressed)

Blank space

Blank space

 

Everything I've researched says something like this should work (Confused as to why it's not):

 

foreach ($mytextarea as $e) {
if ($e == "") {
$e = preg_replace( '/\r\n/', '', $e);
}
}

 

I also thought a string replace would do the trick after checking the length:

 

function validElement($element) {
    return strlen($element) > 1;
}

$mytextarea = array_values(array_filter($mytextarea, "validElement"));
$mytextarea = str_replace("\r",'',$mytextarea; 

 

Neither seem to be working as when I:

count($mytextarea);
echo $mytextarea;

 

I always see the extra 'return' as values in the array.

Link to comment
https://forums.phpfreaks.com/topic/207291-how-to-remove-carriage-return-in-array/
Share on other sites

Thanks Ken, I appreciate it but it didn't work. I discovered that this did for anyone looking:

 

<?PHP
//Bring in Array Data
$mytextarea = explode("\n", $_POST['mytextarea']);

//Remove Return
$mytextarea = str_replace("\r",'',$mytextarea) ;

?>

 

The only difference from my original post, and likely what hindered the function correctly, is that I placed the 'str_replace' right after the $_POST.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.