Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialFrank D
14,042 PointsWhat's the main use of strlen, substr, strpos?
What's the main purpose of strlen, substr, strpos? I mean, what is their main use in php? Can someone give a practical example?
4 Answers
miguelcastro2
Courses Plus Student 6,573 PointsIf I want to see if a password is a minimum of 5 digits long I could use strlen to determine the length of the string. If I want to get the last 4 digits of a social security number I could use substr. Finally, if I want to see if I have a valid email, I could use strpos to see if an @ sign exists in the string.
emanelbaradei
11,395 PointsI know im so late to this, but anyway, i made example for each as previously mentioned examples:
<?php
$email = "myEmail";
$pass = "abcdefghij";
$scurity_num = "12 3456 7890";
$last_nums = substr($scurity_num, 8);
if (strpos($email, "@")) {
echo "Your email is good<br>";
} else {
echo "Your email is wrong, please try again!<br>";
}
if ((strlen($pass) < 5)) {
echo "Your password is wrong, it must be 5 characters or more<br>";
} else if (strlen($pass) >= 5) {
echo "Your password is good<br>";
}
echo "Your card's last 4 numbers are: ** ****$last_nums<br>";
?>
let me know if there is a mistake or something that can be improved. Also, how can we say if we want to make sure that card number contains number and not letters?
madarauchiha
5,355 PointsI think all examples are pretty valid but in a real world scenario, wouldnt you use html5 and javascript and jquery for these kind of things? Perhaps not with the creditcard number for security reasons but otherwise? I read in another topic PHP is mostly used to place and retrieve this kind of data from the database, and further manipulation happens in realtime with the aforementioned languages. Is that a correct assumption?
Mister Moody
48,333 PointsGood work. To ensure that the card contains only numbers, you can implement the ctype_digit()
Function. Read more here
Abhishek Bhardwaj
3,316 Pointsagree with miguelcastro2 and Frank D'Arnese exapmles.
Juan Ordaz
12,012 PointsUsing a server and the way you will want the data to be store, this functions are helpful. Sometimes you don't need to show the whole Middle Name to the client. You can use this functions to just show the Initial.
Frank D
14,042 PointsFrank D
14,042 PointsThank you!! That's really very helpful. Any practical (code) example of that perhaps?
Maybe I came up with one:
martinpatino2
4,741 Pointsmartinpatino2
4,741 PointsGreat example!
Mike Beljak
555 PointsMike Beljak
555 PointsYou teach!
Jordan Howlett
4,377 PointsJordan Howlett
4,377 PointsVery nice.