Solved

Cleaning textarea contents

Posted on 2006-07-05
2
281 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

627 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