Im really stumped on trying to format user input. I have an input box where users enter data separated by commas. Then I explode it:

$data = $_POST['data'];
$words = explode(',', $data);

then I'm analyzing each word. Everything works perfectly when you enter the data correctly. However, if someone typed in "bird, car, train,," or any other similar error it makes my results incorrect.

I'm drawing a blank on how I could build a function to format the user input to make sure there aren't errors like extra commas or other intruding characters.

Any ideas??

Not sure what your input represents, but you could replace all double comma's and remove all spaces.

The input represents terms that are analyzed with a different part of the program. I thought of a function like that but it wouldn't be very efficient to make it only remove double commas. And it can't remove all spaces because some of the words to be analyzed are phrases. such as "rowing, street racing, climbing". Thanks for ur input tho

Then it would be more like this I guess.

$data = $_POST['data'];
$newwords = array ();
$words = explode(',', $data);
foreach ($words as $word)
{
  if (!empty(trim($word)))
  {
    $newwords[] = $word;
  }
}

If it is as simple as finding extra commas and/or whitespace then (using prieas example with one extra line and a new variable):

$data = $_POST['data'];
$newdata = str_replace(",,", ",", trim($data));
$words = explode(',', $newdata);
foreach ($words as $word)
{
  if (!empty(trim($word)))
  {
    $newwords[] = $word;
  }
}

@purplepixie: the loop already takes care of the extra comma's. The str_replace does not remove say ,,,,

pritaeas - Sorry, obviously shouldn't post when I have only been awake for half an hour:-))

Thanks for the ideas.. Pritaeas, I used your example but for some reason it didn't integrate correctly with my code, I ended up getting this to work tho:

$arr = explode(',', $data);
$inc = 0;
	foreach($arr as $trimmed)
	{
		$symp[$inc] = trim($trimmed);
		$inc++;
	}
	$symp_user_view = implode(', ', $symp);

which is pretty much the same concept...
thanks for the help!

Member Avatar for diafol

how's this? it should save you looping

echo trim(preg_replace("/(,)(\s*,\s*)*/",'${1}',$input));

Pritaeas will know better than me, but I think it'll strip extra commas and whitespace, leaving just a single comma.
You could change it to convert ; to , as well and so on.

thanks ardav, i tried your method, however it didn't work with my script, probly cus i have so much stuff going on at once.. anyways i found this method which i think is working the best for me...

$arr = $_POST['input'];
$arr = preg_replace("/[^a-zA-Z 0-9]+/", "", $arr);
$arr = array_filter($arr); 
$inc = 0;

foreach($arr as $trimmed)
{
	$symp[$inc] = trim($trimmed);
	$inc++;
}
Member Avatar for diafol

Hmm, worked for me. why is the variable an array?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.