How to get a substring between two strings in php?
In this post we will show you how to get substring between two strings in PHP.
1. Code:
<?php function getASubstringBetweenTwoStrings($string, $stringStart, $stringEnd){ $string = ' ' . $string; $ini = strpos($string, $stringStart); if ($ini == 0) return ''; $ini += strlen($stringStart); $len = strpos($string, $stringEnd, $ini) - $ini; return substr($string, $ini, $len); } $string = "Soltuts.com1234 providing PHP tutorials, tips, tricks"; echo(getASubstringBetweenTwoStrings($string, "Soltuts", "tips")); ?>
2. Example:
Input:
string = "Soltuts.com1234 providing PHP tutorials, tips, tricks"; StringStart = "Soltuts" stringEnd = "tips"
Output:
Output: ".com1234 providing PHP tutorials, "