WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

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.

  • <?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:

Your Feedback:

We would love to hear from you, please drop us a line.