Signup/Sign In
LAST UPDATED: AUGUST 4, 2021

How to extract substring from a string in PHP?

Answer: Using substr() function

The substr() function is a pre-defined PHP function. This function is used to extract the substring from any given string.

This function takes three parameters that are string, start length, and the length of the extracted string. Out of three parameters, the first two parameters are necessary to specify while the third one is optional. In the first parameter, we have to specify the input string and the second parameter specifies the position at which the function begins to extract the substring from the input string, and the third-string specifies the length of the characters that are to be extracted from the input string.

Syntax

substr($string, $start, $length)

  • $string is the input string

  • $start specifies where to start the string the needs to be extracted. It specifies the position at which the function begins to extract the substring.

  • $length specifies the length of the extracted string. This parameter is optional.

Example

In the given example, we have used the substr() function to extract the substring from the input string. The substring starts with the index 8 and the length of the string is 2.

<!DOCTYPE html>
<html>
<head>
	<title>Extract substring from string</title>
</head>
<body>
	<?php
	$str = "Welcome to Studytonight";
	echo substr($str, 8, 2), "<br>"; 
	?>
</body>
</html>

Output


to

Example: Using substr() function will default length argument

In the even example, we have used the substr() function but do not specify the length of the extracted string. So the output string will start from the index 11 and end at the end of the string.

<!DOCTYPE html>
<html>
<head>
	<title>Extract substring from string</title>
</head>
<body>
	<?php
	$str = "Welcome to Studytonight";
	echo substr($str, 11), "<br>";
	?>
</body>
</html>

Output


Studytonight

Example: Using substr() function with negative value

In the given example, we have specified the negative index for the extracted string. The function returns the string which is at the seventh index from the end of the string.

<!DOCTYPE html>
<html>
<head>
	<title>Extract substring from string</title>
</head>
<body>
	<?php
	$str = "Welcome to Studytonight";
	echo substr($str, -7), "<br>";
	?>
</body>
</html>
 

Output


tonight

Example: Using substr() function with negative length

We can also specify the negative value for the length parameter. In the given example, we have specified the negative value for the length parameter within the substr() function.

<!DOCTYPE html>
<html>
<head>
	<title>Extract substring from string</title>
</head>
<body>
	<?php
	$str = "Welcome to Studytonight";
	echo substr($str, -12, -7 ), "<br>"; 
	?>
</body>
</html>
 

Output


Study

Conclusion

In this lesson, we have discussed how to extract substrings from the string in PHP. To extract the substring from the input string, PHP offers an in-built feature substr() function. This function allows us to extract the string from the given string by specifying the position at which the function begins to extract the substring. We can also specify the length of the substring, and this parameter is optional.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.