From a227397092bb381fea776a798920cfbc3a3e974b Mon Sep 17 00:00:00 2001 From: 0x1881 Date: Tue, 20 Sep 2022 22:46:32 +0300 Subject: [PATCH] getBetween, getBetweens and proxy regex fixed --- src/Curl.php | 60 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 23 deletions(-) diff --git a/src/Curl.php b/src/Curl.php index ec1c2cc..b51ee2d 100644 --- a/src/Curl.php +++ b/src/Curl.php @@ -20,7 +20,7 @@ class Curl /** * Proxy parser regex */ - private const PROXY_REGEX = '/^(?:(?[http|https|socks4|socks5]*?):\/\/)?(?:(?\w+)(?::(?\w*))@)?(?(?!\-)(?:(?:[a-zA-Z\d][a-zA-Z\d\-]{0,61})?[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}|((?:\d{1,3})(?:\.\d{1,3}){3}))(?::(?\d{1,5}))$/ms'; + private const PROXY_REGEX = '/^(?:(?[http|https|socks4|socks5]*?):\/\/)?(?:(?[\w0-9-_]*)(?::(?[\w0-9-_]*))@)?(?(?!\-)(?:(?:[a-zA-Z\d][a-zA-Z\d\-]{0,61})?[a-zA-Z\d]\.){1,126}(?!\d+)[a-zA-Z\d]{1,63}|((?:\d{1,3})(?:\.\d{1,3}){3}))(?::(?\d{1,5}))$/ms'; /** * Curl request method properties @@ -938,17 +938,29 @@ public function contains($search_data, $source = null): bool * @param bool $remove_line_break * @return string */ - public function getBetween(string $start = '', string $end = '', string $source = null, bool $remove_line_break = false): string + public function getBetween(string $start = '', string $end = '', string $source = null, bool $include_delimiters = false, bool $remove_line_break = false, int &$offset = 0): ?string { - if (\is_null($source)) { - $source = $this->getResponse($remove_line_break); - } - $source = ' ' . $source; - $ini = strpos($source, $start); - if ($ini == 0) return ''; - $ini += strlen($start); - $len = strpos($source, $end, $ini) - $ini; - return substr($source, $ini, $len); + if ($source === '' || $start === '' || $end === '') return null; + + if (\is_null($source)) $source = $this->getResponse($remove_line_break); + + $startLength = strlen($start); + $endLength = strlen($end); + + $startPos = strpos($source, $start, $offset); + if ($startPos === false) return null; + + $endPos = strpos($source, $end, $startPos + $startLength); + if ($endPos === false) return null; + + $length = $endPos - $startPos + ($include_delimiters ? $endLength : -$startLength); + if (!$length) return null; + + $offset = $startPos + ($include_delimiters ? 0 : $startLength); + + $result = substr($source, $offset, $length); + + return ($result !== false ? $result : null); } /** @@ -960,20 +972,22 @@ public function getBetween(string $start = '', string $end = '', string $source * @param bool $remove_line_break * @return array */ - public function getBetweens(string $start = '', string $end = '', string $source = null, bool $remove_line_break = false): array + public function getBetweens(string $start = '', string $end = '', string $source = null, bool $include_delimiters = false, bool $remove_line_break = false, int &$offset = 0): ?array { - if (\is_null($source)) { - $source = $this->getResponse($remove_line_break); - } - $n = explode($start, $source); - $result = []; - foreach ($n as $val) { - $pos = strpos($val, $end); - if ($pos !== false) { - $result[] = substr($val, 0, $pos); - } + if (\is_null($source)) $source = $this->getResponse($remove_line_break); + + $strings = []; + $length = strlen($source); + + while ($offset < $length) { + $found = $this->getBetween($start, $end, $source, $include_delimiters, $remove_line_break, $offset); + if ($found === null) break; + + $strings[] = $found; + $offset += strlen($include_delimiters ? $found : $start . $found . $end); // move offset to the end of the newfound string } - return $result ?? []; + + return $strings; } /**