/** * Extracts all the hyperlinks from the entry text. * * @param string $text * @param boolean $allowLocalLinks * @return array of hyperlink in data */ protected function extractLinks($text, $allowLocalLinks = false) { $matches = array(); $result = preg_match_all('/href="([^"]+)"/i', $text, $matches); if (isset($matches[1])) { $urls = $matches[1]; } else { $urls = array(); } $result = array(); foreach ($urls as $url) { if ($allowLocalLinks) { $result[] = $url; } else { $Socket = new HttpSocket(); $urlInfo = $Socket->parseUri($url); $serverHost = ''; if (isset($urlInfo['host']) && isset($urlInfo['scheme'])) { $schemeLen = strlen($urlInfo['scheme']) + 3; $serverHost = $urlInfo['scheme'] . '://' . $urlInfo['host']; if (strpos(substr(FULL_BASE_URL, $schemeLen), ':') !== false) { $serverHost .= ':' . $urlInfo['port']; } } if ($serverHost != FULL_BASE_URL) { $result[] = $url; } } } return $result; }
/** * Parse uri wrapper. Handle both relative and global uri * * @param string $uri * @return array */ public function parseUri($uri) { $sock = new HttpSocket(); $type = 'FULL'; if (strpos($uri, '://') === false) { $localUri = 'http://sample.com' . $uri; $uri = 'http://' . $uri; } $uriArray = @$sock->parseUri($uri); if (!$uriArray) { $uriArray = @$sock->parseUri($localUri); if (!$uriArray) { return false; } else { $type = 'REL'; } } return $uriArray; }