From 16e1000f1cac5f4b0fefcb94badbbf6188c5450c Mon Sep 17 00:00:00 2001 From: myvesta <38690722+myvesta@users.noreply.github.com> Date: Sat, 11 Mar 2023 10:40:40 +0100 Subject: [PATCH] str_get_between PHP function --- func/string.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/func/string.php b/func/string.php index c94f85a2e..111c57eb9 100644 --- a/func/string.php +++ b/func/string.php @@ -11,3 +11,33 @@ function myvesta_replace_in_file($find, $replace, $file) { $r=file_put_contents($file, $buf); return $r; } + +function myvesta_str_get_between (&$text, $left_substring, $right_substring, $start=0, $do_not_return_left_substring=1, $do_not_return_right_substring=1, $left_substring_necessary=1, $right_substring_necessary=1) +{ + $from_null=0; + $pos1=strpos($text, $left_substring, $start); + if ($pos1===FALSE) + { + if ($left_substring_necessary==1) return ""; + $pos1=$start; + $from_null=1; + } + + if ($do_not_return_left_substring==1) + { + if ($from_null==0) $pos1=$pos1+strlen($left_substring); + } + $pos2=strpos($text, $right_substring, $pos1+1); + if ($pos2===FALSE) + { + if ($right_substring_necessary==1) return ""; + $pos2=strlen($text); + } + if ($do_not_return_right_substring==1) $len=$pos2-$pos1; + else $len=($pos2-$pos1)+strlen($right_substring); + + $slen=strlen($text); + if ($pos1+$len>$slen) $len=$slen-$pos1; + + return substr($text, $pos1, $len); +}