Q:

How to extract substring from a string in PHP

0

How to extract substring from a string in PHP

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the PHP substr() function

The PHP substr() function can be used to get the substring i.e. the part of a string from a string. This function takes the start and length parameters to return the portion of string.

Let's check out an example to understand how this function basically works:

<?php
$str = "Hello World!";
echo substr($str, 0, 5);  // Outputs: Hello
echo substr($str, 0, -7); // Outputs: Hello
echo substr($str, 0);     // Outputs: Hello World!
echo substr($str, -6, 5); // Outputs: World
echo substr($str, -6);    // Outputs: World!
echo substr($str, -12);   // Outputs: Hello World!
?>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP  and MySQL Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to compare two strings in PHP... >>
<< How append a string in PHP...