Bootstrap FreeKB - PHP - Cut a string using substr
PHP - Cut a string using substr

Updated:   |  PHP articles

The substr operator can be used to split or cut a string into pieces. For example, let's say you have a variable that contains the text HelloWorld.

$string = "HelloWorld";

 

substr can be used to cut this string into pieces. In this example, the $greeting variable will contain the text "Hello" and the $planet variable will contain the text "World".

  • In the $greeting variable, 0 means "start at the beginning of the string" and 5 means "get five characters".
  • In the $planet variable, 0 means "start at the fifth character of the string" and 5 means "get five characters".

 

$greeting = substr($string, 0, 5);
$planet   = substr($string, 5, 5);

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter fd3670 in the box below so that we can be sure you are a human.