Trimming all white spaces and trailing comma

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Trimming all white spaces and trailing comma

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Trimming all white spaces and trailing comma

Post 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" }
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: Trimming all white spaces and trailing comma

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Trimming all white spaces and trailing comma

Post 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);
(#10850)
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: Trimming all white spaces and trailing comma

Post 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 :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Trimming all white spaces and trailing comma

Post 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.
(#10850)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Trimming all white spaces and trailing comma

Post 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;
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Trimming all white spaces and trailing comma

Post by Christopher »

Benjamin wrote:Loop through array, unset non-integer||invalid entries
That's what preg_split with PREG_SPLIT_NO_EMPTY does...
(#10850)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Trimming all white spaces and trailing comma

Post 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.
Post Reply