Trimming all white spaces and trailing comma
Moderator: General Moderators
Trimming all white spaces and trailing comma
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.
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.
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Trimming all white spaces and trailing comma
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
Re: Trimming all white spaces and trailing comma
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
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
- 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
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 functionsLive24x7 wrote:Thanks - I really did not understand why u suggested pregsplit - but your answer did point me in the correct direction.
Code: Select all
$numbers = preg_split('/[^0-9\.]/', "1 ,3,5,7, ", null, PREG_SPLIT_NO_EMPTY);(#10850)
Re: Trimming all white spaces and trailing comma
Sorry but i did not understand - is there a difference if i use:
instead of

Code: Select all
$numbers = preg_replace('/\s+/', '', $numbers);Code: Select all
$numbers = preg_split('/[^0-9\.]/', "1 ,3,5,7, ", null, PREG_SPLIT_NO_EMPTY);I definitely agree with this. My knowledge of library functions is very limited and i need to improveBut you just need to think more backwards (and know your library functions).
- 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
Write unit tests that use a variety of test values and see which one meets your specification.Live24x7 wrote:Sorry but i did not understand - is there a difference if i use:
(#10850)
Re: Trimming all white spaces and trailing comma
Explode on Commas
array_map trim
Loop through array, unset non-integer||invalid entries
Done.
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;- 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
That's what preg_split with PREG_SPLIT_NO_EMPTY does...Benjamin wrote:Loop through array, unset non-integer||invalid entries
(#10850)
Re: Trimming all white spaces and trailing comma
Sweet, I like that better than a loop.Christopher wrote:That's what preg_split with PREG_SPLIT_NO_EMPTY does...Benjamin wrote:Loop through array, unset non-integer||invalid entries