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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.