6

suppose the value is

1,2,,4,5,6 - For this i can use str_replace(",,",",",$mystring) to get 1,2,4,5,6

How to get values like 2,4,5,6 from ,2,,4,5,6 where two or more consecutive commas are replaced by a single comma and if comma comes before any value it is neglected.If there is only commas like ,,,,,, then empty value is returned.

how to do this in php.

3
  • Try this : str_replace( ',,', ',', 1,2,,4,5,6); Commented Aug 10, 2016 at 3:49
  • Just out of curiosity, were does that string come from? Maybe the problem is how that string is created. Isn't it possible that there could be three commas between numbers? In that case it will only replace one pair of commas and leave two commas. Commented Aug 11, 2016 at 0:29
  • this comma separated value come from input boxes with same name.i save their values using commas.if any input is empty then string will have a empty place followed by comma again.so whenever i need to prefill those values i just run the loop.In preview i have to show only actual value. Commented Aug 11, 2016 at 3:39

4 Answers 4

8

You can explode the string by comma first and then implode after filtering the empty strings.

$val=",2,,4,5,6";
$parts=explode(",",$val);
$parts=array_filter($parts);
echo(implode(",",$parts));

Note that this will also filter 0 from your values. If you want to keep the zeros, refer this question

Sign up to request clarification or add additional context in comments.

1 Comment

Note that array_filter may remove zeroes as well. I'm not sure if the OP wants zeroes or not.
4

You can do what you did on your first problem...

Then use trim() to remove unnecessary commas at the front or end of the string.

$mystring = ',2,,4,5,6';

$output = str_replace(',,', ',', $mystring);

echo trim($output, ',');

//Output will be: 2,4,5,6

Comments

0
// Variable Declaration for String
$str = "1,2,,4,5,6";

// Create Array Out of the String, The comma ',' is the delimiter
// This would output 
//       [ 1 => 1, 2 => 2, 3 => '', 4 => 4, 5 => 5, 6 => 6 ]
$explodedStr = explode(',', $str);

// Filter Array And Remove The empty element which in this case
//    3 => ''
$filteredArray = array_filter( $explodedStr );

// Convert Array into String with comma delimiter 
$formattedString = implode(',',  $filteredArray);

1 Comment

Although this code may help to solve the problem, it doesn't explain why and/or how it answers the question. Providing this additional context would significantly improve its long-term value. Please edit your answer to add explanation, including what limitations and assumptions apply.
0
  1. Trim all commas from the front and back of the string because they are not useful delimiters.
  2. Match one or more commas that occur immediately after a comma.

In my pattern, I match a comma, then forget it with \K, then match one or more commas. These extra commas are then replaced with an empty string.

Code: (Demo)

$string = ',1,2,,4,5,,,,6,,,,';
echo preg_replace('/,\K,+/', '', trim($string, ','));
// 1,2,4,5,6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.