Remove Comma from string that is created using Implode in php
Following code will explain how to remove the comma inside a string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php //handling the implode with null value in the middle of the string with comma $myarry=array('hello','','are','you'); $dataVal = implode(',', $myarry); echo $dataVal;echo "<br>"; //handling the implode with null value in the start of the string with comma $myarry=array('','','are','you'); $dataVal = implode(',', $myarry); $dataVal = preg_replace('/^,+|,+$/', '', $dataVal); echo $dataVal;echo "<br>"; //handling the implode with null value in the middle of the string with multiple commas $myarry=array('hello','','','you'); $dataVal = implode(',', array_filter($myarry)); $dataVal = preg_replace('/^,+|,+$/', '', $dataVal); echo $dataVal; ?> |