Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

Cleaning textarea contents

I created this to remove duplicates and trim each line of a textarea box. And it works.

I am wondering if there is a more efficient way to write this and possibley make it into a function.

// TRIM, CREATE ARRAY, REMOVE DUPLICATES, MAKE STRING, TRIM AGAIN
$array = array_unique(explode("\r\n", trim($field1)));
$cleaned = implode("\r\n", $array);
$trimmed = preg_replace ( "/\s\s+/" , "\r\n" , $cleaned );
echo $trimmed;
0
ray-solomon
Asked:
ray-solomon
1 Solution
 
RWJDComCommented:
It looks pretty good to me.  I guess you could turn it into a function by doing...

function cleanText($field)
{
   $array = array_unique(explode("\r\n", trim($field)));
   $cleaned = implode("\r\n", $array);
   $trimmed = preg_replace ( "/\s\s+/" , "\r\n" , $cleaned );
   return $trimmed;
}

Then you would just run...

$trimmed_variable = cleanText($_POST['textbox']);

Then your $trimmed_variable would be the new text.
0
 
ray-solomonAuthor Commented:
Thanks for confirming this, I implemented this as a function and it helped reduce my 27 lines of code down to 9 lines.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now