0

I have this string :

[{"type":"date","label":"Champ Date de ","className":"form-control","name":"date-1545926599900"}]

I need to delete the last space from the label, the actual value is "Champ Date de " i need to convert it as "Champ Date de".

Here my actual code :

        $form = str_replace('\n','',$request->getParameter('data'));
        $form = str_replace('\t','',$form);
        $form = str_replace(' ','',$form);
        $form = str_replace('<br>','',$form);
        var_dump($form); die;

The problem come from the
: str_replace('<br>','',$form);

with &nbsp; i have no problem and the function str_replace do not add a space. but with <br> the str_replace add a space add i really need to have any space at the end of this value. hope someone could help.

I could have multiple array :

'[{"type":"date","label":"Champ Date de ","className":"form-control","name":"date-1545929424866"},{"type":"checkbox-group","label":"You like it ? ","name":"checkbox-group-1545929428281","values":[{"label":"Option 1","value":"1","selected":true}]}]'
Mathieu Mourareau
  • 1,140
  • 2
  • 23
  • 47
  • 1
    Can you not just `trim($form)` as the last operation? – Michael Berkowski Dec 27 '18 at 16:29
  • 1
    Try [`trim()`](http://us1.php.net/manual/en/function.trim.php), it will remove all white space from the beginning and the end, except for the `
    `
    – aynber Dec 27 '18 at 16:29
  • yes i tried but when i do that i get "ChampDatede" instead of "Champ Date de" – Mathieu Mourareau Dec 27 '18 at 16:30
  • 1
    @MathieuMourareau That is simply imposible... – jeroen Dec 27 '18 at 16:30
  • Though, if you're trying to trim it from the middle of the json, it would be better to use `json_decode`, then go through and trim all the values. – aynber Dec 27 '18 at 16:32
  • 2
    Is there any issue with `json_decode()`-ing it, target the value your wish to trim, apply `trim()`, and `json_encode()` it back to a string? – MonkeyZeus Dec 27 '18 at 16:33
  • I think you are getting the string without space because of all those `str_replace` you are using – DaFois Dec 27 '18 at 16:33
  • 1
    Did you try [``rtrim``](http://www.php.net/manual/en/function.rtrim.php) ? It remove only spaces at the end of your string – Fanie Void Dec 27 '18 at 16:34
  • `str_replace` can use an array, so you only need one call. Do you have the literal characters in your string? `\n` in single quotes is just that, in double quotes it is a new line. Probably https://stackoverflow.com/questions/53948015/php-replace-the-last-space-in-the-label#comment94734596_53948015 is the best idea – user3783243 Dec 27 '18 at 16:34

2 Answers2

3

How about this non-regex way with array_map('trim',$array);?

<?php
$json = '[{"type":"date","label":"Champ Date de ","className":"form-control","name":"date-1545926599900"}]';
$array = json_decode($json,1)[0];
$array= array_map('trim',$array);
echo json_encode([$array]);
?>

Output:

[{"type":"date","label":"Champ Date de","className":"form-control","name":"date-1545926599900"}]

DEMO: https://3v4l.org/qJ9K1

EDIT: As per OP's comment

<?php
$json = '[{"type":"date","label":"Champ Date de ","className":"form-control","name":"date-1545929424866"},{"type":"checkbox-group","label":"You like it ? ","name":"checkbox-group-1545929428281","values":[{"label":"Option 1","value":"1","selected":true}]}]';
$array = json_decode($json,1);
$expected = [];
foreach($array as $k=>$v){
    foreach($v as $key=>$value){
        if($key == 'label' && !is_array($value)){
          $expected[$k][$key]= trim($value); 
       }else{
          $expected[$k][$key]= $value; 
       }
    }
}

echo json_encode($expected);
?>

DEMO: https://3v4l.org/NRlsR

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
0

You can do it without loop by using regex Here is the solution:

$string = '[{"type":"date","label":"Champ Date de ","className":"form-control","name":"date-1545929424866"},{"type":"checkbox-group","label":"You like it ? ","name":"checkbox-group-1545929428281","values":[{"label":"Option 1","value":"1","selected":true}]}]';
$pattern = '/("label":"([\w\s])+)/m';
echo preg_replace_callback($pattern, function($match) {return trim($match[0]);}, $string);

I have done echo for the result. You just need to assign it to a variable.