From 008d191d3c61a0e00f5df8ffc744cfe6577b4759 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 3 Apr 2024 16:30:08 +0300 Subject: [PATCH] add Body::findLinks method --- README.md | 10 ++++++++++ src/Gemtext/Body.php | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/README.md b/README.md index 18d00fb..699c976 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,16 @@ var_dump( ); ``` +#### Body::findLinks + +Find context links by protocol as argument, `gemini` by default + +``` +var_dump( + $body->findLinks('http') // returns array of http links found +); +``` + ### Link Inline links parser. diff --git a/src/Gemtext/Body.php b/src/Gemtext/Body.php index 1a2a0b6..a9d8209 100644 --- a/src/Gemtext/Body.php +++ b/src/Gemtext/Body.php @@ -83,4 +83,26 @@ class Body return $matches; } + + public function findLinks(string $protocol = 'gemini'): array + { + $matches = []; + + foreach ($this->_lines as $line) + { + if (preg_match('/' . $protocol . ':\/\/(.*)[\s\S\'"]*/', trim($line), $match)) + { + $matches[] = + sprintf( + '%s://%s', + $protocol, + trim( + $match[1] + ) + ); + } + } + + return $matches; + } } \ No newline at end of file