Movatterモバイル変換


[0]ホーム

URL:


Translate:
  • Follow Us

  • Pages

  • Find a Job
  • DFP-300×250-1

  • Newsletter Subscription

      Email

      Country


    • Jobboard

    • How would you find out if a string contains another string in PHP?

      Suppose we have a string that is stored in a PHP variable called $aString. And we want to find out if inside of $aString there is another substring – let’s just say for the sake of an example that we are looking for the string “Waldo” inside of the larger string.

      Now let’s say that the name of the larger string ($aString) is
      this: “Where is Waldo?”. And, we just want to find out if $aString contains “Waldo”. PHP provides us with a function calledstrpos that will allow us to find the existence of one string inside of another. Here is an example of how to use the strpos function:

      Example of how to find out if one string contains another in PHP

      if (strpos($aString,'Waldo') !== false) {    echo 'I found Waldo!';}

      But, there is something you should be aware of when using the strpos function: if you are looking for “Waldo” inside a string that looks like this: “heyWaldo are you there?”, then the strpos function will return successfully with the string positon of “Waldo”, basically saying that “Waldo” was indeed found. This is of course a problem if you only want to search for the string “Waldo” as aseparate word, and not as part of another word.

      Strpos never returns true

      One thing about the strpos function that you should remember is that it never returns the boolean value of true. The strpos function returns a value indicating the position of the first occurrence of the substring being searched for. If the substring is not found “false” is returned instead – which is why in the code above we check for false instead of true.

      !== vs != in PHP

      One thing worth noting in the code above is that we used the !== operator instead of the != operator (which has one less “=”). What’s the difference between the 2 operators?

      You can think of the !== operator as being more ‘strict’ than the != operator. This is because the !== operator will say that the two operands being compared are not equal only if thetype of the two operands are the same, but their values arenot equal.

      This is desirable behavior because the strpos function can return a 0 if the string being searched contains the substring as the very first element. The 0 would represent the 0th index of the larger string – meaning the first position in that string. So, if $aString is “Waldo is here”, and we are searching for “Waldo”, then the strpos function will return a 0. This means that the check being performed will be to see if 0 is not equal to false. But the problem is that 0 is also considered as the integer equivalent of the boolean ‘false’ in PHP, which means that the statement “0 != false” will be considered false, because 0 is equal to false in PHP.

      But, if we run “0 !== false” instead, then that statement will be considered to be true, because it just adds the additional check to see if 0 and false are of the sametype. Since 0 is an integer and false is a boolean, clearly they are not equal so comparing the 0 and false forinequality returns true unlike the “0 != false” check, which returns false.

      Hopefully that was not too confusing and if you need more details on that concept you can read about it here:Difference between == and === in PHP.

      if we had this code instead – where we use != and not !== – then it would be a problem:

      Problematic code to find a substring inside a larger string

      if (strpos($aString,'Waldo') != false) {    echo 'I found Waldo!';}

      The code above can result in problems for the reasons discussed above. It’s always better to use !== instead of !=.

      Hiring? Job Hunting? Post a JOB or your RESUME on our JOB BOARD >>

      Subscribe to our newsletter for more free interview questions.

      Follow @programmerintvw
      Previous...
      Next...

      Would you like to thankProgrammerInterview.com for being a helpful free resource?Then why not tell a friend about us, orsimply add a link to this page from your webpage using the HTML below.

      Link to this page:

      Please bookmark with social media, your votes are noticed and appreciated:


      [8]ページ先頭

      ©2009-2025 Movatter.jp