PHP Substring Example

Extract part of a string with substr(). Edit the code below and press Run to execute it with real PHP in your browser.

Code Editor
Output
Full: Hello, World!
First 5: Hello
Last 6: World!
Middle: World
Before comma: Hello

How this substring example works

substr() takes a start offset and an optional length; negative offsets count from the end. strstr($s, $needle, true) returns everything before the first occurrence of the needle.