Php replace the last space in the label












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}]}]'









share|improve this question




















  • 1




    Can you not just trim($form) as the last operation?
    – Michael Berkowski
    Dec 27 '18 at 16:29






  • 1




    Try trim(), it will remove all white space from the beginning and the end, except for the <br>
    – aynber
    Dec 27 '18 at 16:29








  • 1




    @MathieuMourareau That is simply imposible...
    – jeroen
    Dec 27 '18 at 16:30






  • 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






  • 1




    Did you try rtrim ? It remove only spaces at the end of your string
    – Fanie Void
    Dec 27 '18 at 16:34


















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('&nbsp;','',$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}]}]'









share|improve this question




















  • 1




    Can you not just trim($form) as the last operation?
    – Michael Berkowski
    Dec 27 '18 at 16:29






  • 1




    Try trim(), it will remove all white space from the beginning and the end, except for the <br>
    – aynber
    Dec 27 '18 at 16:29








  • 1




    @MathieuMourareau That is simply imposible...
    – jeroen
    Dec 27 '18 at 16:30






  • 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






  • 1




    Did you try rtrim ? It remove only spaces at the end of your string
    – Fanie Void
    Dec 27 '18 at 16:34
















0












0








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('&nbsp;','',$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}]}]'









share|improve this question















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('&nbsp;','',$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}]}]'






php






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 27 '18 at 16:52

























asked Dec 27 '18 at 16:26









Mathieu Mourareau

533522




533522








  • 1




    Can you not just trim($form) as the last operation?
    – Michael Berkowski
    Dec 27 '18 at 16:29






  • 1




    Try trim(), it will remove all white space from the beginning and the end, except for the <br>
    – aynber
    Dec 27 '18 at 16:29








  • 1




    @MathieuMourareau That is simply imposible...
    – jeroen
    Dec 27 '18 at 16:30






  • 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






  • 1




    Did you try rtrim ? It remove only spaces at the end of your string
    – Fanie Void
    Dec 27 '18 at 16:34
















  • 1




    Can you not just trim($form) as the last operation?
    – Michael Berkowski
    Dec 27 '18 at 16:29






  • 1




    Try trim(), it will remove all white space from the beginning and the end, except for the <br>
    – aynber
    Dec 27 '18 at 16:29








  • 1




    @MathieuMourareau That is simply imposible...
    – jeroen
    Dec 27 '18 at 16:30






  • 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






  • 1




    Did you try rtrim ? It remove only spaces at the end of your string
    – Fanie Void
    Dec 27 '18 at 16:34










1




1




Can you not just trim($form) as the last operation?
– Michael Berkowski
Dec 27 '18 at 16:29




Can you not just trim($form) as the last operation?
– Michael Berkowski
Dec 27 '18 at 16:29




1




1




Try trim(), it will remove all white space from the beginning and the end, except for the <br>
– aynber
Dec 27 '18 at 16:29






Try trim(), it will remove all white space from the beginning and the end, except for the <br>
– aynber
Dec 27 '18 at 16:29






1




1




@MathieuMourareau That is simply imposible...
– jeroen
Dec 27 '18 at 16:30




@MathieuMourareau That is simply imposible...
– jeroen
Dec 27 '18 at 16:30




2




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




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




1




1




Did you try rtrim ? It remove only spaces at the end of your string
– Fanie Void
Dec 27 '18 at 16:34






Did you try rtrim ? It remove only spaces at the end of your string
– Fanie Void
Dec 27 '18 at 16:34














2 Answers
2






active

oldest

votes


















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






share|improve this answer























  • thanks for your reply it's working but i can have multiple array. i update the post with the exemple with multiple array
    – Mathieu Mourareau
    Dec 27 '18 at 16:51






  • 1




    @MathieuMourareau then you can do it using 2 foreach(). See my edit and let me know is that worked for you or not now?
    – Curious_Mind
    Dec 28 '18 at 1:39






  • 1




    working !!! many many thanks
    – Mathieu Mourareau
    Dec 28 '18 at 12:32



















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":"([ws])+)/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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53948015%2fphp-replace-the-last-space-in-the-label%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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






    share|improve this answer























    • thanks for your reply it's working but i can have multiple array. i update the post with the exemple with multiple array
      – Mathieu Mourareau
      Dec 27 '18 at 16:51






    • 1




      @MathieuMourareau then you can do it using 2 foreach(). See my edit and let me know is that worked for you or not now?
      – Curious_Mind
      Dec 28 '18 at 1:39






    • 1




      working !!! many many thanks
      – Mathieu Mourareau
      Dec 28 '18 at 12:32
















    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






    share|improve this answer























    • thanks for your reply it's working but i can have multiple array. i update the post with the exemple with multiple array
      – Mathieu Mourareau
      Dec 27 '18 at 16:51






    • 1




      @MathieuMourareau then you can do it using 2 foreach(). See my edit and let me know is that worked for you or not now?
      – Curious_Mind
      Dec 28 '18 at 1:39






    • 1




      working !!! many many thanks
      – Mathieu Mourareau
      Dec 28 '18 at 12:32














    3












    3








    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






    share|improve this answer














    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 28 '18 at 1:38

























    answered Dec 27 '18 at 16:38









    Curious_Mind

    14.5k32443




    14.5k32443












    • thanks for your reply it's working but i can have multiple array. i update the post with the exemple with multiple array
      – Mathieu Mourareau
      Dec 27 '18 at 16:51






    • 1




      @MathieuMourareau then you can do it using 2 foreach(). See my edit and let me know is that worked for you or not now?
      – Curious_Mind
      Dec 28 '18 at 1:39






    • 1




      working !!! many many thanks
      – Mathieu Mourareau
      Dec 28 '18 at 12:32


















    • thanks for your reply it's working but i can have multiple array. i update the post with the exemple with multiple array
      – Mathieu Mourareau
      Dec 27 '18 at 16:51






    • 1




      @MathieuMourareau then you can do it using 2 foreach(). See my edit and let me know is that worked for you or not now?
      – Curious_Mind
      Dec 28 '18 at 1:39






    • 1




      working !!! many many thanks
      – Mathieu Mourareau
      Dec 28 '18 at 12:32
















    thanks for your reply it's working but i can have multiple array. i update the post with the exemple with multiple array
    – Mathieu Mourareau
    Dec 27 '18 at 16:51




    thanks for your reply it's working but i can have multiple array. i update the post with the exemple with multiple array
    – Mathieu Mourareau
    Dec 27 '18 at 16:51




    1




    1




    @MathieuMourareau then you can do it using 2 foreach(). See my edit and let me know is that worked for you or not now?
    – Curious_Mind
    Dec 28 '18 at 1:39




    @MathieuMourareau then you can do it using 2 foreach(). See my edit and let me know is that worked for you or not now?
    – Curious_Mind
    Dec 28 '18 at 1:39




    1




    1




    working !!! many many thanks
    – Mathieu Mourareau
    Dec 28 '18 at 12:32




    working !!! many many thanks
    – Mathieu Mourareau
    Dec 28 '18 at 12:32













    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":"([ws])+)/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.






    share|improve this answer


























      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":"([ws])+)/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.






      share|improve this answer
























        0












        0








        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":"([ws])+)/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.






        share|improve this answer












        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":"([ws])+)/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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 27 '18 at 18:04









        Gagan Prajapati

        1416




        1416






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53948015%2fphp-replace-the-last-space-in-the-label%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Mossoró

            Error while reading .h5 file using the rhdf5 package in R

            Pushsharp Apns notification error: 'InvalidToken'