Skip to content
This repository was archived by the owner on Nov 3, 2025. It is now read-only.

Commit a227397

Browse files
committed
getBetween, getBetweens and proxy regex fixed
1 parent d4c70df commit a227397

1 file changed

Lines changed: 37 additions & 23 deletions

File tree

src/Curl.php

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Curl
2020
/**
2121
* Proxy parser regex
2222
*/
23-
private const PROXY_REGEX = '/^(?:(?<method>[http|https|socks4|socks5]*?):\/\/)?(?:(?<username>\w+)(?::(?<password>\w*))@)?(?<host>(?!\-)(?:(?:[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}))(?::(?<port>\d{1,5}))$/ms';
23+
private const PROXY_REGEX = '/^(?:(?<method>[http|https|socks4|socks5]*?):\/\/)?(?:(?<username>[\w0-9-_]*)(?::(?<password>[\w0-9-_]*))@)?(?<host>(?!\-)(?:(?:[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}))(?::(?<port>\d{1,5}))$/ms';
2424

2525
/**
2626
* Curl request method properties
@@ -938,17 +938,29 @@ public function contains($search_data, $source = null): bool
938938
* @param bool $remove_line_break
939939
* @return string
940940
*/
941-
public function getBetween(string $start = '', string $end = '', string $source = null, bool $remove_line_break = false): string
941+
public function getBetween(string $start = '', string $end = '', string $source = null, bool $include_delimiters = false, bool $remove_line_break = false, int &$offset = 0): ?string
942942
{
943-
if (\is_null($source)) {
944-
$source = $this->getResponse($remove_line_break);
945-
}
946-
$source = ' ' . $source;
947-
$ini = strpos($source, $start);
948-
if ($ini == 0) return '';
949-
$ini += strlen($start);
950-
$len = strpos($source, $end, $ini) - $ini;
951-
return substr($source, $ini, $len);
943+
if ($source === '' || $start === '' || $end === '') return null;
944+
945+
if (\is_null($source)) $source = $this->getResponse($remove_line_break);
946+
947+
$startLength = strlen($start);
948+
$endLength = strlen($end);
949+
950+
$startPos = strpos($source, $start, $offset);
951+
if ($startPos === false) return null;
952+
953+
$endPos = strpos($source, $end, $startPos + $startLength);
954+
if ($endPos === false) return null;
955+
956+
$length = $endPos - $startPos + ($include_delimiters ? $endLength : -$startLength);
957+
if (!$length) return null;
958+
959+
$offset = $startPos + ($include_delimiters ? 0 : $startLength);
960+
961+
$result = substr($source, $offset, $length);
962+
963+
return ($result !== false ? $result : null);
952964
}
953965

954966
/**
@@ -960,20 +972,22 @@ public function getBetween(string $start = '', string $end = '', string $source
960972
* @param bool $remove_line_break
961973
* @return array
962974
*/
963-
public function getBetweens(string $start = '', string $end = '', string $source = null, bool $remove_line_break = false): array
975+
public function getBetweens(string $start = '', string $end = '', string $source = null, bool $include_delimiters = false, bool $remove_line_break = false, int &$offset = 0): ?array
964976
{
965-
if (\is_null($source)) {
966-
$source = $this->getResponse($remove_line_break);
967-
}
968-
$n = explode($start, $source);
969-
$result = [];
970-
foreach ($n as $val) {
971-
$pos = strpos($val, $end);
972-
if ($pos !== false) {
973-
$result[] = substr($val, 0, $pos);
974-
}
977+
if (\is_null($source)) $source = $this->getResponse($remove_line_break);
978+
979+
$strings = [];
980+
$length = strlen($source);
981+
982+
while ($offset < $length) {
983+
$found = $this->getBetween($start, $end, $source, $include_delimiters, $remove_line_break, $offset);
984+
if ($found === null) break;
985+
986+
$strings[] = $found;
987+
$offset += strlen($include_delimiters ? $found : $start . $found . $end); // move offset to the end of the newfound string
975988
}
976-
return $result ?? [];
989+
990+
return $strings;
977991
}
978992

979993
/**

0 commit comments

Comments
 (0)