30秒学会 PHP 片段 · 2019年3月1日

30秒学会 PHP 片段 – firstStringBetween

Returns the first string there is between the strings from the parameter $start and $end.

Use trim() and strstr() to find the string contained between $start and $end.

代码实现

function firstStringBetween($haystack, $start, $end)
{
  return trim(strstr(strstr($haystack, $start), $end, true), $start . $end);
}

使用样例

firstStringBetween('This is a [custom] string', '[', ']'); // custom