/** * Search for a url in the message, * if we find one, then we fetch it * * @param NewsItemEntity $newsItem entity from the DB or the form context * @param NewsItemEntity $data entity from the form * @return NewsItemEntity */ private function searchUrl(NewsItemEntity $newsItem) { $pattern = '/[-a-zA-Z0-9:%_\\+.~#?&\\/\\/=]{2,256}\\.[a-z]{2,10}\\b(\\/[-a-zA-Z0-9:%_\\+.~#?&\\/\\/=]*)?/i'; if (!preg_match($pattern, $newsItem->getMessage(), $matches)) { // No link found, so set link data to null. return $this->unsetUrl($newsItem); } $url = $matches[0]; $result = $this->scrapeUrl($url); if (empty($result)) { return $this->unsetUrl($newsItem); } $newsItem->setLinkUrl(ParserUtil::sanitizeUrl($url)); $newsItem->setLinkTitle($result['title']); $newsItem->setLinkDescription($result['description']); return $newsItem; }