super-powers/src/cefe4092-a4c2-41a6-a683-bd3.../code.power

76 lines
1.7 KiB
Plaintext

/**
* get all strings between two other strings
*
* @param string $content The content to search
* @param string $start The starting value
* @param string $end The ending value
*
* @return array|null On success
* @since 3.0.9
*/
public static function allBetween(string $content, string $start, string $end): ?array
{
// reset bucket
$bucket = [];
for ($i = 0; ; $i++)
{
// search for string
$found = self::between($content, $start, $end);
if (StringHelper::check($found))
{
// add to bucket
$bucket[] = $found;
// build removal string
$remove = $start . $found . $end;
// remove from content
$content = str_replace($remove, '', $content);
}
else
{
break;
}
// safety catch
if ($i == 500)
{
break;
}
}
// only return unique array of values
if (ArrayHelper::check($bucket))
{
return array_unique($bucket);
}
return null;
}
/**
* get a string between two other strings
*
* @param string $content The content to search
* @param string $start The starting value
* @param string $end The ending value
* @param string $default The default value if none found
*
* @return string On success / empty string on failure
* @since 3.0.9
*/
public static function between(string $content, string $start, string $end, string $default = ''): string
{
$array = explode($start, $content);
if (isset($array[1]) && strpos($array[1], $end) !== false)
{
$array = explode($end, $array[1]);
// return string found between
return $array[0];
}
return $default;
}