WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES
Advertisements

How to extract substring from a string in PHP

Topic: PHP / MySQLPrev|Next

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!
?>

Related FAQ

Here are some more FAQ related to this topic:

Advertisements
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties