Solved

Cleaning textarea contents

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
The viewer will learn how to dynamically set the form action using jQuery.

708 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

12 Experts available now in Live!

Get 1:1 Help Now