Get a substring between two strings in PHP
So you need to get a substring between two strings? A quick Google returns about 101,000 results, the first three of which are from StackOverflow and solve the problem for rather specific use-cases. I wanted to solve the problem once-and-for-all in a more general-purpose way.
The "Find Between" Function
/**
* Finds a substring between two strings
* @param string $string The string to be searched
* @param string $start The start of the desired substring
* @param string $end The end of the desired substring
* @param bool $greedy Use last instance of`$end` (default: false)
* @return string
*/
function find_between(string $string, string $start, string $end, bool $greedy = false) {
$start = preg_quote($start, '/');
$end = preg_quote($end, '/');
$format = '/(%s)(.*';
if (!$greedy) $format .= '?';
$format .= ')(%s)/';
$pattern = sprintf($format, $start, $end);
preg_match($pattern, $string, $matches);
return $matches[2];
}
If you're running PHP 5.6 or lower you'll need to remove the typehints
Nothing too flashy here, it escapes the start and end strings, constructs a regex pattern and returns whatever comes between.
Usage Example
$string = 'fizz foo bar foo foo';
$start = 'foo';
$end = 'foo';
$greedy = false;
var_dump(find_between($string, $start, $end));
// string(5) " bar "
By default it'll find the shortest string possible (i.e. stop at the first instance of the end string) but if you pass true to $greedy then it'll keep looking until it finds the last instance of the end string.
For example: foo bar foo foo with foo as both start and end string, greedy will return bar foo and non-greedy will return only bar.
A note on WordPress
A common complaint in the WordPress community is the inability to nest shortcodes of the same name, e.g. [short][short]Hello, World![/short][/short] because the regex which parses content for shortcodes is lazy and will stop at the first closing tag.
Assuming [short] wraps its contents in a bold tag, you'd get something like <b>[short]Hello, World!</b>[/short] as the output because the inner shortcode doesn't get passed to the handling function in the $content parameter as you'd expect it to.
It's a frustrating behaviour and leads developers to roll their own solutions, such as registering multiple shortcodes (e.g. [short], [short-inner], etc.) or even to register shortcodes dynamically if one is found in the content with a given prefix (e.g. [short], [short-foo], [short-bar], etc.) but that's not particularly user-friendly, defying the whole point of shortcodes.
This might be fixed in the future (see https://core.trac.wordpress.org/ticket/14481) but for now, you'll have to roll your own solution too.
Conclusion
While this is by no means a new problem and the solution is hardly revolutionary, it at least provides a super-simple way to get the job done. No more writing regex by hand (and no more substr-ing, explode-ing and implode-ing!) just a clear and simple function to solve a common annoyance.