See if one string contains another string












2














I use the following code to check if a string is the session username at the moment.



if($_SESSION['username'] == $home || $_SESSION['username'] == $away)


I am looking to change that to see if the string includes the session username, not specifically IS the username.



Is there a way to do this?
Thankyou



Edit :



Say the string is "Username1 and Username2", I will "Username1" to be found.



I have done the following:



if( ( strpos($home, $_SESSION['username']) !== false)  || ( strpos($away, $_SESSION['username']) !== false) )


That doesnt appear to have worked though!










share|improve this question





























    2














    I use the following code to check if a string is the session username at the moment.



    if($_SESSION['username'] == $home || $_SESSION['username'] == $away)


    I am looking to change that to see if the string includes the session username, not specifically IS the username.



    Is there a way to do this?
    Thankyou



    Edit :



    Say the string is "Username1 and Username2", I will "Username1" to be found.



    I have done the following:



    if( ( strpos($home, $_SESSION['username']) !== false)  || ( strpos($away, $_SESSION['username']) !== false) )


    That doesnt appear to have worked though!










    share|improve this question



























      2












      2








      2


      1





      I use the following code to check if a string is the session username at the moment.



      if($_SESSION['username'] == $home || $_SESSION['username'] == $away)


      I am looking to change that to see if the string includes the session username, not specifically IS the username.



      Is there a way to do this?
      Thankyou



      Edit :



      Say the string is "Username1 and Username2", I will "Username1" to be found.



      I have done the following:



      if( ( strpos($home, $_SESSION['username']) !== false)  || ( strpos($away, $_SESSION['username']) !== false) )


      That doesnt appear to have worked though!










      share|improve this question















      I use the following code to check if a string is the session username at the moment.



      if($_SESSION['username'] == $home || $_SESSION['username'] == $away)


      I am looking to change that to see if the string includes the session username, not specifically IS the username.



      Is there a way to do this?
      Thankyou



      Edit :



      Say the string is "Username1 and Username2", I will "Username1" to be found.



      I have done the following:



      if( ( strpos($home, $_SESSION['username']) !== false)  || ( strpos($away, $_SESSION['username']) !== false) )


      That doesnt appear to have worked though!







      php






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 28 '11 at 19:19









      user7116

      54.4k15123159




      54.4k15123159










      asked Jul 30 '10 at 15:02









      sark9012

      2,395124779




      2,395124779
























          2 Answers
          2






          active

          oldest

          votes


















          7














          One way, of many, would be to use the strpos() function, which the documentation says is the fastest way to just determine if a substring occurs within a string.



          if (strpos($_SESSION['username'],$home) !== false)


          The format of strpos is strpos(*haystack*, *needle*). So, the above would be true if $_SESSION['username'] is Username1 and $home is Username1 and Username2.



          If you actually need the substring back (rather than a position), strstr() is a good way to go.






          share|improve this answer























          • +1, but please add the link to the PHP doc.
            – Bob Fincheimer
            Jul 30 '10 at 15:06










          • Best to add !== FALSE, your example fails if the string is in the beginning.
            – Blizz
            Jul 30 '10 at 15:07










          • Wouldn't this fail if the session username is at position 0
            – Neil Aitken
            Jul 30 '10 at 15:07










          • @Bob, Thanks for the +! Both function names are linked to the PHP docs.
            – Mark Biek
            Jul 30 '10 at 15:07










          • Good point Blizz & Neil. I've updated accordingly.
            – Mark Biek
            Jul 30 '10 at 15:11



















          1














          This would work



          $username = 'theusername';

          if(strpos($username,$_SESSION['username']) !== false) {
          // contains username
          }





          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%2f3372722%2fsee-if-one-string-contains-another-string%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









            7














            One way, of many, would be to use the strpos() function, which the documentation says is the fastest way to just determine if a substring occurs within a string.



            if (strpos($_SESSION['username'],$home) !== false)


            The format of strpos is strpos(*haystack*, *needle*). So, the above would be true if $_SESSION['username'] is Username1 and $home is Username1 and Username2.



            If you actually need the substring back (rather than a position), strstr() is a good way to go.






            share|improve this answer























            • +1, but please add the link to the PHP doc.
              – Bob Fincheimer
              Jul 30 '10 at 15:06










            • Best to add !== FALSE, your example fails if the string is in the beginning.
              – Blizz
              Jul 30 '10 at 15:07










            • Wouldn't this fail if the session username is at position 0
              – Neil Aitken
              Jul 30 '10 at 15:07










            • @Bob, Thanks for the +! Both function names are linked to the PHP docs.
              – Mark Biek
              Jul 30 '10 at 15:07










            • Good point Blizz & Neil. I've updated accordingly.
              – Mark Biek
              Jul 30 '10 at 15:11
















            7














            One way, of many, would be to use the strpos() function, which the documentation says is the fastest way to just determine if a substring occurs within a string.



            if (strpos($_SESSION['username'],$home) !== false)


            The format of strpos is strpos(*haystack*, *needle*). So, the above would be true if $_SESSION['username'] is Username1 and $home is Username1 and Username2.



            If you actually need the substring back (rather than a position), strstr() is a good way to go.






            share|improve this answer























            • +1, but please add the link to the PHP doc.
              – Bob Fincheimer
              Jul 30 '10 at 15:06










            • Best to add !== FALSE, your example fails if the string is in the beginning.
              – Blizz
              Jul 30 '10 at 15:07










            • Wouldn't this fail if the session username is at position 0
              – Neil Aitken
              Jul 30 '10 at 15:07










            • @Bob, Thanks for the +! Both function names are linked to the PHP docs.
              – Mark Biek
              Jul 30 '10 at 15:07










            • Good point Blizz & Neil. I've updated accordingly.
              – Mark Biek
              Jul 30 '10 at 15:11














            7












            7








            7






            One way, of many, would be to use the strpos() function, which the documentation says is the fastest way to just determine if a substring occurs within a string.



            if (strpos($_SESSION['username'],$home) !== false)


            The format of strpos is strpos(*haystack*, *needle*). So, the above would be true if $_SESSION['username'] is Username1 and $home is Username1 and Username2.



            If you actually need the substring back (rather than a position), strstr() is a good way to go.






            share|improve this answer














            One way, of many, would be to use the strpos() function, which the documentation says is the fastest way to just determine if a substring occurs within a string.



            if (strpos($_SESSION['username'],$home) !== false)


            The format of strpos is strpos(*haystack*, *needle*). So, the above would be true if $_SESSION['username'] is Username1 and $home is Username1 and Username2.



            If you actually need the substring back (rather than a position), strstr() is a good way to go.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 at 3:22









            Luke Peterson

            5,89083340




            5,89083340










            answered Jul 30 '10 at 15:04









            Mark Biek

            90.9k50144193




            90.9k50144193












            • +1, but please add the link to the PHP doc.
              – Bob Fincheimer
              Jul 30 '10 at 15:06










            • Best to add !== FALSE, your example fails if the string is in the beginning.
              – Blizz
              Jul 30 '10 at 15:07










            • Wouldn't this fail if the session username is at position 0
              – Neil Aitken
              Jul 30 '10 at 15:07










            • @Bob, Thanks for the +! Both function names are linked to the PHP docs.
              – Mark Biek
              Jul 30 '10 at 15:07










            • Good point Blizz & Neil. I've updated accordingly.
              – Mark Biek
              Jul 30 '10 at 15:11


















            • +1, but please add the link to the PHP doc.
              – Bob Fincheimer
              Jul 30 '10 at 15:06










            • Best to add !== FALSE, your example fails if the string is in the beginning.
              – Blizz
              Jul 30 '10 at 15:07










            • Wouldn't this fail if the session username is at position 0
              – Neil Aitken
              Jul 30 '10 at 15:07










            • @Bob, Thanks for the +! Both function names are linked to the PHP docs.
              – Mark Biek
              Jul 30 '10 at 15:07










            • Good point Blizz & Neil. I've updated accordingly.
              – Mark Biek
              Jul 30 '10 at 15:11
















            +1, but please add the link to the PHP doc.
            – Bob Fincheimer
            Jul 30 '10 at 15:06




            +1, but please add the link to the PHP doc.
            – Bob Fincheimer
            Jul 30 '10 at 15:06












            Best to add !== FALSE, your example fails if the string is in the beginning.
            – Blizz
            Jul 30 '10 at 15:07




            Best to add !== FALSE, your example fails if the string is in the beginning.
            – Blizz
            Jul 30 '10 at 15:07












            Wouldn't this fail if the session username is at position 0
            – Neil Aitken
            Jul 30 '10 at 15:07




            Wouldn't this fail if the session username is at position 0
            – Neil Aitken
            Jul 30 '10 at 15:07












            @Bob, Thanks for the +! Both function names are linked to the PHP docs.
            – Mark Biek
            Jul 30 '10 at 15:07




            @Bob, Thanks for the +! Both function names are linked to the PHP docs.
            – Mark Biek
            Jul 30 '10 at 15:07












            Good point Blizz & Neil. I've updated accordingly.
            – Mark Biek
            Jul 30 '10 at 15:11




            Good point Blizz & Neil. I've updated accordingly.
            – Mark Biek
            Jul 30 '10 at 15:11













            1














            This would work



            $username = 'theusername';

            if(strpos($username,$_SESSION['username']) !== false) {
            // contains username
            }





            share|improve this answer




























              1














              This would work



              $username = 'theusername';

              if(strpos($username,$_SESSION['username']) !== false) {
              // contains username
              }





              share|improve this answer


























                1












                1








                1






                This would work



                $username = 'theusername';

                if(strpos($username,$_SESSION['username']) !== false) {
                // contains username
                }





                share|improve this answer














                This would work



                $username = 'theusername';

                if(strpos($username,$_SESSION['username']) !== false) {
                // contains username
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 9 '15 at 16:47

























                answered Jul 30 '10 at 15:05









                Neil Aitken

                6,81633440




                6,81633440






























                    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%2f3372722%2fsee-if-one-string-contains-another-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

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    Refactoring coordinates for Minecraft Pi buildings written in Python