PHP Strings Accessing Substrings - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Strings Accessing Substrings - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Sunday, May 5, 2019

PHP Strings Accessing Substrings

PHP strings




Accessing Substrings


Problem

You want to know if a string contains a particular substring. For example, you want to find out if an email address contains a @.

Solution


Example  Finding a substring with strpos()

         if (strpos ($_POST['email'], '@') === false)  {

                 print  'There was no @ in the e-mail address!';
         }

Discussion

The return value from Strpos() is the first position in the string (the  "haystack") at which the substring (the "needle") was found. If the needle wasn't found at all in the haystack, strpos() returns false. If the needle is at the beginning of the haystack, strpos() returns 0 because positon 0 represents the beginning of the string. To ferentiate between return values of 0 and false, you must use the identity oper (===) or the not-identity operator (!==) instead of regular equals (==) or not-eq (!=). 

Example  compares the return value from strpos () returns 0 or any other number.


No comments:

Post a Comment

Post Top Ad