Page 1 of 1

Trimming all white spaces and trailing comma

Posted: Thu Aug 16, 2012 2:45 am
by Live24x7
A form field accepts numbers separated by comma from the users like 1,6,73,99 (there could be any number of comma separated inputs)

Before this list is processed - i am using trim
$numbers = $_POST['numbers'];
$numbers = trim("$numbers");

this works fine - but if i enter a space before comma - this does not work.
so 1 ,6,73,99 does not work (notice space after 1 and before ',')
My question: How do i correct this input ?

Another question
How do i ignore the trailing comma - so if a user enters
1,6,73,99, - ignore the comma after 99.

Re: Trimming all white spaces and trailing comma

Posted: Thu Aug 16, 2012 8:45 am
by social_experiment
You could try preg_split(). Once you have the array you can check for empty elements to weed out the spaces.

Code: Select all

<?php
 $data = "1 ,3,5,7";
 
 $string = preg_split('/[\s,]/', $data);
 
 var_dump($string);
 // array(5) { [0]=> string(1) "1" [1]=> string(0) "" [2]=> string(1) "3" [3]=> string(1) "5" [4]=> string(1) "7" }

Re: Trimming all white spaces and trailing comma

Posted: Thu Aug 16, 2012 10:50 pm
by Live24x7
Thanks - I really did not understand why u suggested pregsplit - but your answer did point me in the correct direction.

I used $numbers = preg_replace('/\s+/', '', $numbers); and this did the trick to remove all white spaces.

However i have still not been able to figure out how to remove the trailing comma - if some user enters it.

thanks a lot

Re: Trimming all white spaces and trailing comma

Posted: Thu Aug 16, 2012 11:10 pm
by Christopher
Live24x7 wrote:Thanks - I really did not understand why u suggested pregsplit - but your answer did point me in the correct direction.
You should know that social_experiment was on the right track. If the thing you are trying to find is variable then you need to use the preg_ rather than the str_ functions. But you just need to think more backwards (and know your library functions ;)).

Code: Select all

$numbers = preg_split('/[^0-9\.]/', "1 ,3,5,7, ", null, PREG_SPLIT_NO_EMPTY);

Re: Trimming all white spaces and trailing comma

Posted: Sat Aug 18, 2012 1:17 am
by Live24x7
Sorry but i did not understand - is there a difference if i use:

Code: Select all

$numbers = preg_replace('/\s+/', '', $numbers);
instead of

Code: Select all

$numbers = preg_split('/[^0-9\.]/', "1 ,3,5,7, ", null, PREG_SPLIT_NO_EMPTY);
But you just need to think more backwards (and know your library functions ;)).
I definitely agree with this. My knowledge of library functions is very limited and i need to improve :)

Re: Trimming all white spaces and trailing comma

Posted: Sat Aug 18, 2012 12:58 pm
by Christopher
Live24x7 wrote:Sorry but i did not understand - is there a difference if i use:
Write unit tests that use a variety of test values and see which one meets your specification.

Re: Trimming all white spaces and trailing comma

Posted: Sat Aug 18, 2012 1:41 pm
by Benjamin
Explode on Commas
array_map trim
Loop through array, unset non-integer||invalid entries
Done.

Code: Select all

<?php
$string = "1,2,a, 5,6 ,7,";
$array = array_map('trim', explode(',', $string));

foreach ($array as $k => $v) {
    if (!preg_match('#^\d+$#', $v)) {
        unset($array[$k]);
    }
}

$string = implode(',', $array);

echo $string;

Re: Trimming all white spaces and trailing comma

Posted: Sun Aug 19, 2012 12:02 pm
by Christopher
Benjamin wrote:Loop through array, unset non-integer||invalid entries
That's what preg_split with PREG_SPLIT_NO_EMPTY does...

Re: Trimming all white spaces and trailing comma

Posted: Sun Aug 19, 2012 12:05 pm
by Benjamin
Christopher wrote:
Benjamin wrote:Loop through array, unset non-integer||invalid entries
That's what preg_split with PREG_SPLIT_NO_EMPTY does...
Sweet, I like that better than a loop.