Remove String in Limit Maximum












-1















I have a string that has 14 characters TEST,TEST,TEST



the problem is I want to limit the string becomes 7 characters and store to DB



** I have tried this code:**



$text = "TEST,TEST,TEST";
DB::table('table')
->update([
'name' => substr($text, 0, 7);
]);


but the result like this TEST,TE, I don't want to show last cut character, I want to the result like this TES if not up to 7 characters.



I want to limit every string that insert to database just 7 characters, if from 7 characters there are words that are truncated like 'TE', 'T', 'TES' I don't want to display it, so only the full words are displayed that is 'TEST'.










share|improve this question

























  • What if you try this 'name' => substr($text, 0,3);

    – Ayaz Shah
    Nov 14 '18 at 9:21











  • you mean 6 char + ',' = 7 char?

    – scaisEdge
    Nov 14 '18 at 9:22











  • You want to limit your string to 6 characters but are expecting "TES" as output? Am I missing something? Can you explain a little more?

    – kerbholz
    Nov 14 '18 at 9:24











  • sorry i have edited my question

    – Dewa Aditya
    Nov 14 '18 at 9:27











  • which character I don't want to show last cut character

    – Bhargav Chudasama
    Nov 14 '18 at 9:28


















-1















I have a string that has 14 characters TEST,TEST,TEST



the problem is I want to limit the string becomes 7 characters and store to DB



** I have tried this code:**



$text = "TEST,TEST,TEST";
DB::table('table')
->update([
'name' => substr($text, 0, 7);
]);


but the result like this TEST,TE, I don't want to show last cut character, I want to the result like this TES if not up to 7 characters.



I want to limit every string that insert to database just 7 characters, if from 7 characters there are words that are truncated like 'TE', 'T', 'TES' I don't want to display it, so only the full words are displayed that is 'TEST'.










share|improve this question

























  • What if you try this 'name' => substr($text, 0,3);

    – Ayaz Shah
    Nov 14 '18 at 9:21











  • you mean 6 char + ',' = 7 char?

    – scaisEdge
    Nov 14 '18 at 9:22











  • You want to limit your string to 6 characters but are expecting "TES" as output? Am I missing something? Can you explain a little more?

    – kerbholz
    Nov 14 '18 at 9:24











  • sorry i have edited my question

    – Dewa Aditya
    Nov 14 '18 at 9:27











  • which character I don't want to show last cut character

    – Bhargav Chudasama
    Nov 14 '18 at 9:28
















-1












-1








-1








I have a string that has 14 characters TEST,TEST,TEST



the problem is I want to limit the string becomes 7 characters and store to DB



** I have tried this code:**



$text = "TEST,TEST,TEST";
DB::table('table')
->update([
'name' => substr($text, 0, 7);
]);


but the result like this TEST,TE, I don't want to show last cut character, I want to the result like this TES if not up to 7 characters.



I want to limit every string that insert to database just 7 characters, if from 7 characters there are words that are truncated like 'TE', 'T', 'TES' I don't want to display it, so only the full words are displayed that is 'TEST'.










share|improve this question
















I have a string that has 14 characters TEST,TEST,TEST



the problem is I want to limit the string becomes 7 characters and store to DB



** I have tried this code:**



$text = "TEST,TEST,TEST";
DB::table('table')
->update([
'name' => substr($text, 0, 7);
]);


but the result like this TEST,TE, I don't want to show last cut character, I want to the result like this TES if not up to 7 characters.



I want to limit every string that insert to database just 7 characters, if from 7 characters there are words that are truncated like 'TE', 'T', 'TES' I don't want to display it, so only the full words are displayed that is 'TEST'.







php laravel-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 9:38







Dewa Aditya

















asked Nov 14 '18 at 9:18









Dewa AdityaDewa Aditya

77




77













  • What if you try this 'name' => substr($text, 0,3);

    – Ayaz Shah
    Nov 14 '18 at 9:21











  • you mean 6 char + ',' = 7 char?

    – scaisEdge
    Nov 14 '18 at 9:22











  • You want to limit your string to 6 characters but are expecting "TES" as output? Am I missing something? Can you explain a little more?

    – kerbholz
    Nov 14 '18 at 9:24











  • sorry i have edited my question

    – Dewa Aditya
    Nov 14 '18 at 9:27











  • which character I don't want to show last cut character

    – Bhargav Chudasama
    Nov 14 '18 at 9:28





















  • What if you try this 'name' => substr($text, 0,3);

    – Ayaz Shah
    Nov 14 '18 at 9:21











  • you mean 6 char + ',' = 7 char?

    – scaisEdge
    Nov 14 '18 at 9:22











  • You want to limit your string to 6 characters but are expecting "TES" as output? Am I missing something? Can you explain a little more?

    – kerbholz
    Nov 14 '18 at 9:24











  • sorry i have edited my question

    – Dewa Aditya
    Nov 14 '18 at 9:27











  • which character I don't want to show last cut character

    – Bhargav Chudasama
    Nov 14 '18 at 9:28



















What if you try this 'name' => substr($text, 0,3);

– Ayaz Shah
Nov 14 '18 at 9:21





What if you try this 'name' => substr($text, 0,3);

– Ayaz Shah
Nov 14 '18 at 9:21













you mean 6 char + ',' = 7 char?

– scaisEdge
Nov 14 '18 at 9:22





you mean 6 char + ',' = 7 char?

– scaisEdge
Nov 14 '18 at 9:22













You want to limit your string to 6 characters but are expecting "TES" as output? Am I missing something? Can you explain a little more?

– kerbholz
Nov 14 '18 at 9:24





You want to limit your string to 6 characters but are expecting "TES" as output? Am I missing something? Can you explain a little more?

– kerbholz
Nov 14 '18 at 9:24













sorry i have edited my question

– Dewa Aditya
Nov 14 '18 at 9:27





sorry i have edited my question

– Dewa Aditya
Nov 14 '18 at 9:27













which character I don't want to show last cut character

– Bhargav Chudasama
Nov 14 '18 at 9:28







which character I don't want to show last cut character

– Bhargav Chudasama
Nov 14 '18 at 9:28














2 Answers
2






active

oldest

votes


















1














You can handle this at PHP side and then use it in UPDATE query.



$text = "TEST,TEST,TEST";

$exp = explode(",", $text); // Create an array

$final_string = '';

foreach($exp as $v)
{
//Check when new value is added and string length is less than 7, concat it
if(strlen($final_string) + strlen($v) <= 7)
{
$final_string .= "$v,";
}
}

$final_string = trim($final_string, ",");

echo $final_string; // OUTPUT: TEST


It will work for dynamic strings too. For e.g. your string is T,TEST,TEST it will return T,TEST.




Please note this solution will work if you have comma separated values.




To make it work with spaces, comma and other characters, replace explode with preg_split as follows,



$exp = preg_split("/[s,]+/", $text);





share|improve this answer


























  • What if it's a space delimited list of words.

    – Andreas
    Nov 14 '18 at 9:41











  • OP hasn't specified about spaces. Seems its a comma separated string only. Have added a note in my answer that it will work if you need it for comma separated strings.

    – Samir
    Nov 14 '18 at 9:44











  • Updated my answer to make it work with spaces and any other character desired.

    – Samir
    Nov 14 '18 at 9:52











  • thank you bro, you are amazing.

    – Dewa Aditya
    Nov 14 '18 at 9:59











  • You're welcome. Happy coding :)

    – Samir
    Nov 14 '18 at 10:02



















0














If you only need one delimiter you can use explode:



$text = 'TEST,TEST,TEST';
$word = explode(',', substr($text, 0, 7))[0];


If you need multiple delimiters you can use preg_split and add your delimiters in the regular expression, space and comma in this example:



$text = "TEST,TEST,TEST";
$word = preg_split("/[s,]+/", substr($text, 0, 7))[0];





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%2f53296661%2fremove-string-in-limit-maximum%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









    1














    You can handle this at PHP side and then use it in UPDATE query.



    $text = "TEST,TEST,TEST";

    $exp = explode(",", $text); // Create an array

    $final_string = '';

    foreach($exp as $v)
    {
    //Check when new value is added and string length is less than 7, concat it
    if(strlen($final_string) + strlen($v) <= 7)
    {
    $final_string .= "$v,";
    }
    }

    $final_string = trim($final_string, ",");

    echo $final_string; // OUTPUT: TEST


    It will work for dynamic strings too. For e.g. your string is T,TEST,TEST it will return T,TEST.




    Please note this solution will work if you have comma separated values.




    To make it work with spaces, comma and other characters, replace explode with preg_split as follows,



    $exp = preg_split("/[s,]+/", $text);





    share|improve this answer


























    • What if it's a space delimited list of words.

      – Andreas
      Nov 14 '18 at 9:41











    • OP hasn't specified about spaces. Seems its a comma separated string only. Have added a note in my answer that it will work if you need it for comma separated strings.

      – Samir
      Nov 14 '18 at 9:44











    • Updated my answer to make it work with spaces and any other character desired.

      – Samir
      Nov 14 '18 at 9:52











    • thank you bro, you are amazing.

      – Dewa Aditya
      Nov 14 '18 at 9:59











    • You're welcome. Happy coding :)

      – Samir
      Nov 14 '18 at 10:02
















    1














    You can handle this at PHP side and then use it in UPDATE query.



    $text = "TEST,TEST,TEST";

    $exp = explode(",", $text); // Create an array

    $final_string = '';

    foreach($exp as $v)
    {
    //Check when new value is added and string length is less than 7, concat it
    if(strlen($final_string) + strlen($v) <= 7)
    {
    $final_string .= "$v,";
    }
    }

    $final_string = trim($final_string, ",");

    echo $final_string; // OUTPUT: TEST


    It will work for dynamic strings too. For e.g. your string is T,TEST,TEST it will return T,TEST.




    Please note this solution will work if you have comma separated values.




    To make it work with spaces, comma and other characters, replace explode with preg_split as follows,



    $exp = preg_split("/[s,]+/", $text);





    share|improve this answer


























    • What if it's a space delimited list of words.

      – Andreas
      Nov 14 '18 at 9:41











    • OP hasn't specified about spaces. Seems its a comma separated string only. Have added a note in my answer that it will work if you need it for comma separated strings.

      – Samir
      Nov 14 '18 at 9:44











    • Updated my answer to make it work with spaces and any other character desired.

      – Samir
      Nov 14 '18 at 9:52











    • thank you bro, you are amazing.

      – Dewa Aditya
      Nov 14 '18 at 9:59











    • You're welcome. Happy coding :)

      – Samir
      Nov 14 '18 at 10:02














    1












    1








    1







    You can handle this at PHP side and then use it in UPDATE query.



    $text = "TEST,TEST,TEST";

    $exp = explode(",", $text); // Create an array

    $final_string = '';

    foreach($exp as $v)
    {
    //Check when new value is added and string length is less than 7, concat it
    if(strlen($final_string) + strlen($v) <= 7)
    {
    $final_string .= "$v,";
    }
    }

    $final_string = trim($final_string, ",");

    echo $final_string; // OUTPUT: TEST


    It will work for dynamic strings too. For e.g. your string is T,TEST,TEST it will return T,TEST.




    Please note this solution will work if you have comma separated values.




    To make it work with spaces, comma and other characters, replace explode with preg_split as follows,



    $exp = preg_split("/[s,]+/", $text);





    share|improve this answer















    You can handle this at PHP side and then use it in UPDATE query.



    $text = "TEST,TEST,TEST";

    $exp = explode(",", $text); // Create an array

    $final_string = '';

    foreach($exp as $v)
    {
    //Check when new value is added and string length is less than 7, concat it
    if(strlen($final_string) + strlen($v) <= 7)
    {
    $final_string .= "$v,";
    }
    }

    $final_string = trim($final_string, ",");

    echo $final_string; // OUTPUT: TEST


    It will work for dynamic strings too. For e.g. your string is T,TEST,TEST it will return T,TEST.




    Please note this solution will work if you have comma separated values.




    To make it work with spaces, comma and other characters, replace explode with preg_split as follows,



    $exp = preg_split("/[s,]+/", $text);






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 9:52

























    answered Nov 14 '18 at 9:40









    SamirSamir

    5,3042628




    5,3042628













    • What if it's a space delimited list of words.

      – Andreas
      Nov 14 '18 at 9:41











    • OP hasn't specified about spaces. Seems its a comma separated string only. Have added a note in my answer that it will work if you need it for comma separated strings.

      – Samir
      Nov 14 '18 at 9:44











    • Updated my answer to make it work with spaces and any other character desired.

      – Samir
      Nov 14 '18 at 9:52











    • thank you bro, you are amazing.

      – Dewa Aditya
      Nov 14 '18 at 9:59











    • You're welcome. Happy coding :)

      – Samir
      Nov 14 '18 at 10:02



















    • What if it's a space delimited list of words.

      – Andreas
      Nov 14 '18 at 9:41











    • OP hasn't specified about spaces. Seems its a comma separated string only. Have added a note in my answer that it will work if you need it for comma separated strings.

      – Samir
      Nov 14 '18 at 9:44











    • Updated my answer to make it work with spaces and any other character desired.

      – Samir
      Nov 14 '18 at 9:52











    • thank you bro, you are amazing.

      – Dewa Aditya
      Nov 14 '18 at 9:59











    • You're welcome. Happy coding :)

      – Samir
      Nov 14 '18 at 10:02

















    What if it's a space delimited list of words.

    – Andreas
    Nov 14 '18 at 9:41





    What if it's a space delimited list of words.

    – Andreas
    Nov 14 '18 at 9:41













    OP hasn't specified about spaces. Seems its a comma separated string only. Have added a note in my answer that it will work if you need it for comma separated strings.

    – Samir
    Nov 14 '18 at 9:44





    OP hasn't specified about spaces. Seems its a comma separated string only. Have added a note in my answer that it will work if you need it for comma separated strings.

    – Samir
    Nov 14 '18 at 9:44













    Updated my answer to make it work with spaces and any other character desired.

    – Samir
    Nov 14 '18 at 9:52





    Updated my answer to make it work with spaces and any other character desired.

    – Samir
    Nov 14 '18 at 9:52













    thank you bro, you are amazing.

    – Dewa Aditya
    Nov 14 '18 at 9:59





    thank you bro, you are amazing.

    – Dewa Aditya
    Nov 14 '18 at 9:59













    You're welcome. Happy coding :)

    – Samir
    Nov 14 '18 at 10:02





    You're welcome. Happy coding :)

    – Samir
    Nov 14 '18 at 10:02













    0














    If you only need one delimiter you can use explode:



    $text = 'TEST,TEST,TEST';
    $word = explode(',', substr($text, 0, 7))[0];


    If you need multiple delimiters you can use preg_split and add your delimiters in the regular expression, space and comma in this example:



    $text = "TEST,TEST,TEST";
    $word = preg_split("/[s,]+/", substr($text, 0, 7))[0];





    share|improve this answer




























      0














      If you only need one delimiter you can use explode:



      $text = 'TEST,TEST,TEST';
      $word = explode(',', substr($text, 0, 7))[0];


      If you need multiple delimiters you can use preg_split and add your delimiters in the regular expression, space and comma in this example:



      $text = "TEST,TEST,TEST";
      $word = preg_split("/[s,]+/", substr($text, 0, 7))[0];





      share|improve this answer


























        0












        0








        0







        If you only need one delimiter you can use explode:



        $text = 'TEST,TEST,TEST';
        $word = explode(',', substr($text, 0, 7))[0];


        If you need multiple delimiters you can use preg_split and add your delimiters in the regular expression, space and comma in this example:



        $text = "TEST,TEST,TEST";
        $word = preg_split("/[s,]+/", substr($text, 0, 7))[0];





        share|improve this answer













        If you only need one delimiter you can use explode:



        $text = 'TEST,TEST,TEST';
        $word = explode(',', substr($text, 0, 7))[0];


        If you need multiple delimiters you can use preg_split and add your delimiters in the regular expression, space and comma in this example:



        $text = "TEST,TEST,TEST";
        $word = preg_split("/[s,]+/", substr($text, 0, 7))[0];






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 9:51









        RemulRemul

        1,1581213




        1,1581213






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296661%2fremove-string-in-limit-maximum%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

            Bressuire

            Vorschmack

            Quarantine