Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cleaning textarea contents

Posted on 2006-07-05
2
Medium Priority
?
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
RWJDCom earned 500 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

730 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