How to remove exact match from string?How can I prevent SQL injection in PHP?PHP and EnumerationsDeleting an element from an array in PHPHow do I create a simple 'Hello World' module in Magento?How do I get a YouTube video thumbnail from the YouTube API?How to remove part of a string?Reference — What does this symbol mean in PHP?Remove new lines from string and replace with one empty spaceHow do I check if a string contains a specific word?Remove the last character from string

How to remove ambiguity: "... lives in the city of H, the capital of the province of NS, WHERE the unemployment rate is ..."?

How do I call a 6 digit Austrailian phone number with a US based mobile phone?

How make an image of my entire usb flash drive?

What are these funnel-looking green things in my yard?

Telephone number in spoken words

Why is the result of ('b'+'a'+ + 'a' + 'a').toLowerCase() 'banana'?

Is it possible to grow new organs through exposure to radioactivity?

Markov-chain sentence generator in Python

Can the IPA represent all languages' tones?

How do some PhD students get 10+ papers? Is that what I need for landing good faculty position?

How many samples should I use?

The cat exchanges places with a drawing of the cat

Simplification of numbers

The cat ate your input again!

Why did Saruman lie?

How are you supposed to know the strumming pattern for a song from the "chord sheet music"?

Why does tag require braces while frac doesn't?

How many British prisoners of war were taken by the Wehrmacht and how many died?

How should I deal with a potential date who wouldn’t take no for an answer?

How exactly are corporate bonds priced at issue

Is it okay for a ticket seller to grab a tip in the USA?

My cat is a houdini

Is there a command to install basic applications on Ubuntu 16.04?

What is a good class if we remove subclasses?



How to remove exact match from string?


How can I prevent SQL injection in PHP?PHP and EnumerationsDeleting an element from an array in PHPHow do I create a simple 'Hello World' module in Magento?How do I get a YouTube video thumbnail from the YouTube API?How to remove part of a string?Reference — What does this symbol mean in PHP?Remove new lines from string and replace with one empty spaceHow do I check if a string contains a specific word?Remove the last character from string






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















How i can replace/remove exact match word from string, an example string



$string = 'Hello world, command data test com';


How to remove com ( exact match from the string ) but to don't remove the com from command to










share|improve this question



















  • 1





    str_replace is removing com from command to, this not helping

    – Matei Zoc
    Mar 27 at 10:16

















2















How i can replace/remove exact match word from string, an example string



$string = 'Hello world, command data test com';


How to remove com ( exact match from the string ) but to don't remove the com from command to










share|improve this question



















  • 1





    str_replace is removing com from command to, this not helping

    – Matei Zoc
    Mar 27 at 10:16













2












2








2








How i can replace/remove exact match word from string, an example string



$string = 'Hello world, command data test com';


How to remove com ( exact match from the string ) but to don't remove the com from command to










share|improve this question














How i can replace/remove exact match word from string, an example string



$string = 'Hello world, command data test com';


How to remove com ( exact match from the string ) but to don't remove the com from command to







php






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 27 at 10:14









Matei ZocMatei Zoc

9302 gold badges8 silver badges13 bronze badges




9302 gold badges8 silver badges13 bronze badges










  • 1





    str_replace is removing com from command to, this not helping

    – Matei Zoc
    Mar 27 at 10:16












  • 1





    str_replace is removing com from command to, this not helping

    – Matei Zoc
    Mar 27 at 10:16







1




1





str_replace is removing com from command to, this not helping

– Matei Zoc
Mar 27 at 10:16





str_replace is removing com from command to, this not helping

– Matei Zoc
Mar 27 at 10:16












3 Answers
3






active

oldest

votes


















6














You need to use preg_replace. Basically preg_replace() searches the subject for pattern matches and replaces them with the replacement.



<?php
$string = 'Hello world, command data test com';
$string = preg_replace('/bcomb/', '', $string);
echo $string;
?>


Explanation: Above example pattern is explained below



b: Match a word boundary.
com: Text to match.



For more Special Character Definitions check this






share|improve this answer



























  • Thank you! this is what i was need!

    – Matei Zoc
    Mar 27 at 10:25











  • You're welcome!

    – Shanteshwar Inde
    Mar 27 at 10:25











  • This can easily done by str_replace() .

    – Smartpal
    Mar 27 at 10:26











  • @Smartpal i don't think so, str_replace() replace command to mand

    – Shanteshwar Inde
    Mar 27 at 10:27











  • @ShanteshwarInde Yeah that's.... Sorry i forgot com in command. Up Voted

    – Smartpal
    Mar 27 at 10:32


















3














Short version:



join(' ',array_diff(explode(' ', $string), ['com']));


Explanation:




  • explode(' ', $string) splits your string into an array of words


  • array_diff($words, ['com']) removes the elements on the second array from the first array. So in case the array of $words contains the word com, it will be removed


  • join(' ', $words) concats all the strings in the $words array, dividing each word from each other with a space.

Full snippet:



<?php
$string = 'Hello world, command data test com';
$words = explode(' ', $string);
$words = array_diff($words, ['com']);
$string = join(' ', $words);





share|improve this answer



























  • even though my answer has more upvotes, but this one is my favourite! just add explanation so more people understand. Thanks!

    – Shanteshwar Inde
    Mar 27 at 10:36











  • I honestly think your one is better! Ahahah I will add some better explaination

    – gbalduzzi
    Mar 27 at 10:41


















1














Another version....
If your input string alway in this order You can do it with rtrim



Snippet



$string = 'Hello world, command data test com';
$string = rtrim($string, ' com');
echo $string;


Output



Hello world, command data test


Live demo



Docs
rtrim






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%2f55374717%2fhow-to-remove-exact-match-from-string%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    You need to use preg_replace. Basically preg_replace() searches the subject for pattern matches and replaces them with the replacement.



    <?php
    $string = 'Hello world, command data test com';
    $string = preg_replace('/bcomb/', '', $string);
    echo $string;
    ?>


    Explanation: Above example pattern is explained below



    b: Match a word boundary.
    com: Text to match.



    For more Special Character Definitions check this






    share|improve this answer



























    • Thank you! this is what i was need!

      – Matei Zoc
      Mar 27 at 10:25











    • You're welcome!

      – Shanteshwar Inde
      Mar 27 at 10:25











    • This can easily done by str_replace() .

      – Smartpal
      Mar 27 at 10:26











    • @Smartpal i don't think so, str_replace() replace command to mand

      – Shanteshwar Inde
      Mar 27 at 10:27











    • @ShanteshwarInde Yeah that's.... Sorry i forgot com in command. Up Voted

      – Smartpal
      Mar 27 at 10:32















    6














    You need to use preg_replace. Basically preg_replace() searches the subject for pattern matches and replaces them with the replacement.



    <?php
    $string = 'Hello world, command data test com';
    $string = preg_replace('/bcomb/', '', $string);
    echo $string;
    ?>


    Explanation: Above example pattern is explained below



    b: Match a word boundary.
    com: Text to match.



    For more Special Character Definitions check this






    share|improve this answer



























    • Thank you! this is what i was need!

      – Matei Zoc
      Mar 27 at 10:25











    • You're welcome!

      – Shanteshwar Inde
      Mar 27 at 10:25











    • This can easily done by str_replace() .

      – Smartpal
      Mar 27 at 10:26











    • @Smartpal i don't think so, str_replace() replace command to mand

      – Shanteshwar Inde
      Mar 27 at 10:27











    • @ShanteshwarInde Yeah that's.... Sorry i forgot com in command. Up Voted

      – Smartpal
      Mar 27 at 10:32













    6












    6








    6







    You need to use preg_replace. Basically preg_replace() searches the subject for pattern matches and replaces them with the replacement.



    <?php
    $string = 'Hello world, command data test com';
    $string = preg_replace('/bcomb/', '', $string);
    echo $string;
    ?>


    Explanation: Above example pattern is explained below



    b: Match a word boundary.
    com: Text to match.



    For more Special Character Definitions check this






    share|improve this answer















    You need to use preg_replace. Basically preg_replace() searches the subject for pattern matches and replaces them with the replacement.



    <?php
    $string = 'Hello world, command data test com';
    $string = preg_replace('/bcomb/', '', $string);
    echo $string;
    ?>


    Explanation: Above example pattern is explained below



    b: Match a word boundary.
    com: Text to match.



    For more Special Character Definitions check this







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jul 12 at 4:53

























    answered Mar 27 at 10:22









    Shanteshwar IndeShanteshwar Inde

    1,1742 gold badges11 silver badges20 bronze badges




    1,1742 gold badges11 silver badges20 bronze badges















    • Thank you! this is what i was need!

      – Matei Zoc
      Mar 27 at 10:25











    • You're welcome!

      – Shanteshwar Inde
      Mar 27 at 10:25











    • This can easily done by str_replace() .

      – Smartpal
      Mar 27 at 10:26











    • @Smartpal i don't think so, str_replace() replace command to mand

      – Shanteshwar Inde
      Mar 27 at 10:27











    • @ShanteshwarInde Yeah that's.... Sorry i forgot com in command. Up Voted

      – Smartpal
      Mar 27 at 10:32

















    • Thank you! this is what i was need!

      – Matei Zoc
      Mar 27 at 10:25











    • You're welcome!

      – Shanteshwar Inde
      Mar 27 at 10:25











    • This can easily done by str_replace() .

      – Smartpal
      Mar 27 at 10:26











    • @Smartpal i don't think so, str_replace() replace command to mand

      – Shanteshwar Inde
      Mar 27 at 10:27











    • @ShanteshwarInde Yeah that's.... Sorry i forgot com in command. Up Voted

      – Smartpal
      Mar 27 at 10:32
















    Thank you! this is what i was need!

    – Matei Zoc
    Mar 27 at 10:25





    Thank you! this is what i was need!

    – Matei Zoc
    Mar 27 at 10:25













    You're welcome!

    – Shanteshwar Inde
    Mar 27 at 10:25





    You're welcome!

    – Shanteshwar Inde
    Mar 27 at 10:25













    This can easily done by str_replace() .

    – Smartpal
    Mar 27 at 10:26





    This can easily done by str_replace() .

    – Smartpal
    Mar 27 at 10:26













    @Smartpal i don't think so, str_replace() replace command to mand

    – Shanteshwar Inde
    Mar 27 at 10:27





    @Smartpal i don't think so, str_replace() replace command to mand

    – Shanteshwar Inde
    Mar 27 at 10:27













    @ShanteshwarInde Yeah that's.... Sorry i forgot com in command. Up Voted

    – Smartpal
    Mar 27 at 10:32





    @ShanteshwarInde Yeah that's.... Sorry i forgot com in command. Up Voted

    – Smartpal
    Mar 27 at 10:32













    3














    Short version:



    join(' ',array_diff(explode(' ', $string), ['com']));


    Explanation:




    • explode(' ', $string) splits your string into an array of words


    • array_diff($words, ['com']) removes the elements on the second array from the first array. So in case the array of $words contains the word com, it will be removed


    • join(' ', $words) concats all the strings in the $words array, dividing each word from each other with a space.

    Full snippet:



    <?php
    $string = 'Hello world, command data test com';
    $words = explode(' ', $string);
    $words = array_diff($words, ['com']);
    $string = join(' ', $words);





    share|improve this answer



























    • even though my answer has more upvotes, but this one is my favourite! just add explanation so more people understand. Thanks!

      – Shanteshwar Inde
      Mar 27 at 10:36











    • I honestly think your one is better! Ahahah I will add some better explaination

      – gbalduzzi
      Mar 27 at 10:41















    3














    Short version:



    join(' ',array_diff(explode(' ', $string), ['com']));


    Explanation:




    • explode(' ', $string) splits your string into an array of words


    • array_diff($words, ['com']) removes the elements on the second array from the first array. So in case the array of $words contains the word com, it will be removed


    • join(' ', $words) concats all the strings in the $words array, dividing each word from each other with a space.

    Full snippet:



    <?php
    $string = 'Hello world, command data test com';
    $words = explode(' ', $string);
    $words = array_diff($words, ['com']);
    $string = join(' ', $words);





    share|improve this answer



























    • even though my answer has more upvotes, but this one is my favourite! just add explanation so more people understand. Thanks!

      – Shanteshwar Inde
      Mar 27 at 10:36











    • I honestly think your one is better! Ahahah I will add some better explaination

      – gbalduzzi
      Mar 27 at 10:41













    3












    3








    3







    Short version:



    join(' ',array_diff(explode(' ', $string), ['com']));


    Explanation:




    • explode(' ', $string) splits your string into an array of words


    • array_diff($words, ['com']) removes the elements on the second array from the first array. So in case the array of $words contains the word com, it will be removed


    • join(' ', $words) concats all the strings in the $words array, dividing each word from each other with a space.

    Full snippet:



    <?php
    $string = 'Hello world, command data test com';
    $words = explode(' ', $string);
    $words = array_diff($words, ['com']);
    $string = join(' ', $words);





    share|improve this answer















    Short version:



    join(' ',array_diff(explode(' ', $string), ['com']));


    Explanation:




    • explode(' ', $string) splits your string into an array of words


    • array_diff($words, ['com']) removes the elements on the second array from the first array. So in case the array of $words contains the word com, it will be removed


    • join(' ', $words) concats all the strings in the $words array, dividing each word from each other with a space.

    Full snippet:



    <?php
    $string = 'Hello world, command data test com';
    $words = explode(' ', $string);
    $words = array_diff($words, ['com']);
    $string = join(' ', $words);






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 27 at 10:46

























    answered Mar 27 at 10:23









    gbalduzzigbalduzzi

    2,7718 silver badges26 bronze badges




    2,7718 silver badges26 bronze badges















    • even though my answer has more upvotes, but this one is my favourite! just add explanation so more people understand. Thanks!

      – Shanteshwar Inde
      Mar 27 at 10:36











    • I honestly think your one is better! Ahahah I will add some better explaination

      – gbalduzzi
      Mar 27 at 10:41

















    • even though my answer has more upvotes, but this one is my favourite! just add explanation so more people understand. Thanks!

      – Shanteshwar Inde
      Mar 27 at 10:36











    • I honestly think your one is better! Ahahah I will add some better explaination

      – gbalduzzi
      Mar 27 at 10:41
















    even though my answer has more upvotes, but this one is my favourite! just add explanation so more people understand. Thanks!

    – Shanteshwar Inde
    Mar 27 at 10:36





    even though my answer has more upvotes, but this one is my favourite! just add explanation so more people understand. Thanks!

    – Shanteshwar Inde
    Mar 27 at 10:36













    I honestly think your one is better! Ahahah I will add some better explaination

    – gbalduzzi
    Mar 27 at 10:41





    I honestly think your one is better! Ahahah I will add some better explaination

    – gbalduzzi
    Mar 27 at 10:41











    1














    Another version....
    If your input string alway in this order You can do it with rtrim



    Snippet



    $string = 'Hello world, command data test com';
    $string = rtrim($string, ' com');
    echo $string;


    Output



    Hello world, command data test


    Live demo



    Docs
    rtrim






    share|improve this answer





























      1














      Another version....
      If your input string alway in this order You can do it with rtrim



      Snippet



      $string = 'Hello world, command data test com';
      $string = rtrim($string, ' com');
      echo $string;


      Output



      Hello world, command data test


      Live demo



      Docs
      rtrim






      share|improve this answer



























        1












        1








        1







        Another version....
        If your input string alway in this order You can do it with rtrim



        Snippet



        $string = 'Hello world, command data test com';
        $string = rtrim($string, ' com');
        echo $string;


        Output



        Hello world, command data test


        Live demo



        Docs
        rtrim






        share|improve this answer













        Another version....
        If your input string alway in this order You can do it with rtrim



        Snippet



        $string = 'Hello world, command data test com';
        $string = rtrim($string, ' com');
        echo $string;


        Output



        Hello world, command data test


        Live demo



        Docs
        rtrim







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 10:40









        SmartpalSmartpal

        1,0801 gold badge6 silver badges17 bronze badges




        1,0801 gold badge6 silver badges17 bronze badges






























            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%2f55374717%2fhow-to-remove-exact-match-from-string%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

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해