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 trial

PHP PHP Functions PHP Internal Functions PHP String Functions

Frank D
Frank D
14,042 Points

What'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

If 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.

Frank D
Frank D
14,042 Points

Thank you!! That's really very helpful. Any practical (code) example of that perhaps?

Maybe I came up with one:

<?php

            $card_number = "01 2345 6789";

            $digits = substr($card_number, 8);
            echo "The last digits numbers of your card are: ** **** " . $digits;
        ?>

Great example!

You teach!

Very nice.

emanelbaradei
emanelbaradei
11,395 Points

I 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?

I 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?

Good work. To ensure that the card contains only numbers, you can implement the ctype_digit() Function. Read more here

Abhishek Bhardwaj
Abhishek Bhardwaj
3,316 Points

agree with miguelcastro2 and Frank D'Arnese exapmles.

Juan Ordaz
Juan Ordaz
12,012 Points

Using 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.