Home > Tutorials > PHP Tutorials > String Manipulation – Using SUBSTR & STRLEN PHP Functions

String Manipulation – Using SUBSTR & STRLEN PHP Functions

Now that we’ve covered the basics of what functions are within PHP – PHP has a wide array of functions already available for you to use to work with various strings. So let’s say that we have a variable holding a user’s telephone number and we’re asking our user to input the last 4 digits of their phone number to verify themselves. To do this we’re going to use the help of the substr (substring) function.

$phone = "212-123-4567";
$phone_lastFour = substr($phone, 8, 4);

The PHP substring function takes in 3 total arguments. First off is the string you want to get the substring from, $phone in our above example. Next is the index of the first character to be extracted – or essentially the count of where the first character to be utilized is inside the string. In our example since the phone number format has a total of 12 characters and we only want the last 4 – our first character index is 8. Finally comes the length parameter which specifies the length of the substring you wish to extract. This is the only optional parameter within the substr function as if you leave the length argument out then the returned substring will go all the way to the end of the string. For the purpose of this example we will specify that we want only 4 characters – however we can technically leave this blank as-well.

Now let’s make use of our previous php tutorials covering variable scope, and if/else statements to write the remainder of our script:

<?php
$phone = "212-123-4567";
$phone_lastFour = substr($phone, 8, 4);

if (!empty($_POST))
{ 
	$last4Verify = $_POST["last4Verify"]; 
if ($last4Verify == $phone_lastFour) {
	echo "Congratulations, you're verified!";
} else {
	
	echo '<div align="center">
	Something doesn\'t seem right. Please try again. <br>
		<form method="post">
			<input type="text" name="last4Verify" placeholder="Last 4 of Phone Number";>
			<input type="submit">
		</form>
	</div>';
}

} else {
echo '<div align="center">
		<form method="post">
			<input type="text" name="last4Verify" placeholder="Last 4 of Phone Number";>
			<input type="submit">
		</form>
	</div>';
}
?>


So the first thing we are doing is creating a simple if/else statement to check if the $POST supervariable is set. If it is set then it’s safe to assume the form has been submitted as it is sending data via the POST method. So we take our submitted information and store that into a variable called $last4Verify. Now we simply create an additional if/else statement to check to see if the information that our visitor has entered matches that which we have. If it doesn’t we produce an error message and the form again; alas if it does we give them a success message.

However there is a slight issue we have left. A phone number can be written in many different ways.

  • 212-123-4567
  • 1212-123-4567
  • 1-212-123-4567
  • 2121234567

In all of the above examples the last 4 digits remain the same ofcourse, however the length of the string changes and as such specifying an index for the first of the 4 characters is impossible. Luckily the substr function lets you count from the back.

$phone = "2121234567";
$phone_lastFour = substr($phone, -4, 4);

Here we’re telling PHP we want to get the substring starting from the 4th character from the end. However there is an alternative way also. We can use yet another string function of PHP called strlen which finds the length of a certain string. We can then always find our starting index by subtracting 4 from the length of our string.

$phone = "2121234567";
$length = strlen($phone);
$lastfourLength = $length - 4;
$phone_lastFour = substr($phone, $lastfourLength, 4);

About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

7 Free Content Tools to Boost Your Marketing

Not too long ago, blogs were better known as online personal diaries or journals open ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.

Close