Skip to content

emka.web.id

Menu
  • Home
  • Indeks Artikel
  • Tutorial
  • Tentang Kami
Menu

PHP Cookbook: Accessing Substrings

Posted on December 11, 2011 by Syauqi Wiryahasana
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 Use strpos() [sourcecode language="php"] <?php if (strpos($_POST['email'], '@') === false) { print 'There was no @ in the e-mail address!'; } ?> [/sourcecode] 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, since position 0 represents the beginning of the string. To differentiate between return values of 0 and false, you must use the identity operator (===) or the not–identity operator (!==) instead of regular equals (==) or not-equals (!=). Example 1-8 compares the return value from strpos( ) to false using ===. This test only succeeds if strpos returns false, not if it returns 0 or any other number. See Also Documentation on strpos( ) at http://www.php.net/strpos
Seedbacklink

Recent Posts

TENTANG EMKA.WEB>ID

EMKA.WEB.ID adalah blog seputar teknologi informasi, edukasi dan ke-NU-an yang hadir sejak tahun 2011. Kontak: kontak@emka.web.id.

©2024 emka.web.id Proudly powered by wpStatically