X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FContent%2FText%2FPlaintext.php;h=3b692fd9654794162d9c17c06af2e5c79016b675;hb=947cc98cc43a82cb3e67b51a897009a7bd2629a0;hp=eb17fd0a26da262cbef3afff8f2d0228773770a0;hpb=14b048ba851daf8d6424628426019241380781d6;p=friendica.git diff --git a/src/Content/Text/Plaintext.php b/src/Content/Text/Plaintext.php index eb17fd0a26..3b692fd965 100644 --- a/src/Content/Text/Plaintext.php +++ b/src/Content/Text/Plaintext.php @@ -34,4 +34,41 @@ class Plaintext return $msg; } + + /** + * Returns the character positions of the provided boundaries, optionally skipping a number of first occurrences + * + * @param string $text Text to search + * @param string $open Left boundary + * @param string $close Right boundary + * @param int $occurrences Number of first occurrences to skip + * @return boolean|array + */ + public static function getBoundariesPosition($text, $open, $close, $occurrences = 0) + { + if ($occurrences < 0) { + $occurrences = 0; + } + + $start_pos = -1; + for ($i = 0; $i <= $occurrences; $i++) { + if ($start_pos !== false) { + $start_pos = strpos($text, $open, $start_pos + 1); + } + } + + if ($start_pos === false) { + return false; + } + + $end_pos = strpos($text, $close, $start_pos); + + if ($end_pos === false) { + return false; + } + + $res = ['start' => $start_pos, 'end' => $end_pos]; + + return $res; + } }