Solved

Cleaning textarea contents

Posted on 2006-07-05
2
271 Views
Last Modified: 2011-10-03
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
Comment
Question by:ray-solomon
2 Comments
 
LVL 8

Accepted Solution

by:
RWJDCom earned 125 total points
ID: 17047816
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
 
LVL 10

Author Comment

by:ray-solomon
ID: 17047905
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now