1

I have a problem - hope you can help.

My users will enter a string like this:

'dt', 'time_hour', 'loc', 'protocol_category', 'service_identifier', 'mcc', 'imsi', 'service_dl_bytes', 'service_ul_ bytes'

Depending on their other inputs, I then remove two fields from this list (for example, service_identifier and service_dl_bytes)

This leaves lots of stray commas in the string.

I need some logic that says: - there can only be one consecutive comma - there should not be a comma as the last character - comma, space comma is also not permitted

Basically, the format has to be 'input', 'input2', 'input3'

Can anyone help me .. i tried with the below, but it doesn't work in all use cases

elseif ($df3aggrequired == "YES" and $df3agg2required == "NO" ) {
    #remove spaces from select statement
    $df3group0v2 = str_replace($df3aggfield, '', $df3select);
    #replace aggfield1 with null    
    $df3group1v2 = str_replace(' ', '', $df3group0v2);
    #replace instances of ,, with ,
    $df3group3v2 = preg_replace('/,,+/', ',', $df3group1v2);
    $finalstring0df3v2 = rtrim($df3group3v2, ',');
    $finalstring1df3v2 = str_replace('\'\'', '', $finalstring0df3v2);
    $finalstringdf3v2 = str_replace('.,', '', $finalstring1df3v2);
    $finalstringdf31v2 = str_replace(',,', ',', $finalstringdf3v2);
    $finalcleanup = preg_replace('/,,+/', ',', $finalstringdf31v2);
    echo"\\<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.groupBy(";
    echo "$finalcleanup";
    echo ")";
revo
  • 47,783
  • 14
  • 74
  • 117

5 Answers5

1

I also think that string replacements can cause issues, so went for a similar process to geoidesic, this code tries to stick with the fields being a CSV list, and removes the quotes round them as part of the decoding, but also puts them back again in the result.

$fields = "'dt', 'time_hour', 'loc', 'protocol_category', 'service_identifier', 'mcc', 'imsi', 'service_dl_bytes', 'service_ul_ bytes'";

$removeFields = true ;

$fieldList = str_getcsv($fields, ",", "'");
if ( $removeFields == true )    {
    if ( ($key = array_search('loc', $fieldList)) !== false )   {
        unset ( $fieldList[$key] );
    }

    $fields = "'".implode("', '", $fieldList)."'";
}

echo $fields;

The example removes the 'loc' field, but this can be modified to remove any fields required.

The final output is (assuming 'loc' is removed)...

'dt', 'time_hour', 'protocol_category', 'service_identifier', 'mcc', 'imsi', 'service_dl_bytes', 'service_ul_ bytes'
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

Whatever your string after removing process is, you may want to run a single preg_replace over it using a positive lookahead:

^\s*(,\s*)*|,\s*(?=,|$)

Live demo

Breakdown:

  • ^\s*(,\s*)* Match unwanted commas at beginning
  • | Or
  • ,\s*(?=,|$) Match consecutive commas or trailing ones

PHP code:

$string = preg_replace('~^\s*(,\s*)*|,\s*(?=,|$)~', '', $string);

You'd better consider removing unwanted commas while removing words.

revo
  • 47,783
  • 14
  • 74
  • 117
0

If it is a comma-separated list, then don't use str_replace. Turn the string into an array first by splitting it on the commas:

$myArray = explode(',' $myString);

Then remove the parts of the array you don't want and make it a string again:

$fieldsToDelete = ["service_identifier", "service_dl_bytes"];
$newArray = [];
foreach($myArray as $key => $value) {
   if(!in_array($value, $fieldsToDelete) {
      array_push($newArray, $value)  
   }
}
$newString = implode(',', $newArray)
geoidesic
  • 4,649
  • 3
  • 39
  • 59
0

The following code works on the index of the fields you want to remove.

Works the same as the answer written by @geoidesic but uses index of field in string instead of a string match.

$user_input = "'dt', 'time_hour', 'loc', 'protocol_category', 'service_identifier', 'mcc', 'imsi', 'service_dl_bytes', 'service_ul_ bytes'";
$input_array = explode(',', $user_input);

// 4 = 'service_indentifier'
// 8 = 'service_ul_ bytes' 
$remove_input = array(4, 8);

foreach($remove_input as $array_index) {
    unset($input_array[$array_index]);
}

$parsed_input = implode(',', $input_array);

var_dump($parsed_input);

// Output: string(80) "'dt', 'time_hour', 'loc', 'protocol_category', 'mcc', 'imsi', 'service_dl_bytes'"
Greg Klaus
  • 109
  • 5
0

You have try with

$var = preg_replace(["/'[\s,]*,[\s,]*'/", "/^[\s,]*|[\s,]*$/"], ["', '", ""], $var);

It can help!