How to Extract a Substring from a String in PHP?

In this tutorial, you will learn how to extract a substring from a string in PHP. To extract a substring, we can use the substr() function.

What is the syntax of substr() function?

substr(original text,position,number of characters)

Here substr() function contains three arguments. The first argument will specify the source string. The second argument will specify the index from where the substring will be extracted. The third argument will specify the length of the extracted substring.

Here is an example of extracting a substring "cloud" from a string "Cloudnet in India".

<html>
<body>
<?php 
$a="Cloudnet in India";
$b=substr($a,0,5);//here 5 characters will be extracted from the index 0
echo $b;

?>
</body>
</html>

Output: Cloud

See also:

Leave a Comment