php

How to extract a substring from a string in PHP

In this tutorial, we are going to see how to extract a substring from a string in PHP. substr() function in PHP can be used to get the substring(part of a string). This function takes the start index and length parameters to return the substring from a string.
 

Exemple 1:
<?php
	$str = "Welcom To StackHowTo!";
	echo substr($str, 0, 6);
?>

Output:

Welcom
 

Exemple 2:

Here, we start from the end of the string.

<?php
	$str = "Welcom To StackHowTo!";
	echo substr($str, -13, 12);
?>

Output:

StackHowTo
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *