Zola Posted February 4, 2014 Share Posted February 4, 2014 Hi I am learning PHP on codecademy at the moment. Enjoying it well and going right back to the basics to try to get a fundamental understanding At the moment, I am learning about functions (strlen, strpos) etc. etc. This may be a stupid question, but can anyone please give me a real world example of where these functions may be used? I guess strlen could be used to allow or disallow a certain length of a user name or something, but what use would strpos have? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted February 4, 2014 Share Posted February 4, 2014 (edited) strlen returns the length of the string you pass to it. You could use this as you mentioned to check a username matches a certain length. if(strlen($username) < 5) { echo 'Your username is too short. 5 Characters minimum'; } strpos is used for finding the position of a character within a string. A weak example of this would to check if an email address is valid by checking for the @ char if(strpos($emailAddress, '@') === false) { echo 'Your email address is invalid'; } Edited February 4, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
jazzman1 Posted February 4, 2014 Share Posted February 4, 2014 @Ch0cu3r, but...not in a situation where the string contains non-english characters <?php // $username = "jazz"; $username = 'джаз'; // cyrillic characters if(strlen($username) < 5) { echo 'Failed'; } else { echo 'Past'; } In case like this, he would consider using Multibyte String Functons expecialy mb_strlen and mb_strpos. Quote Link to comment Share on other sites More sharing options...
Zola Posted February 4, 2014 Author Share Posted February 4, 2014 Many thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.