protected function External::getPageTitle in Freelinking 4.0.x
Same name and namespace in other branches
- 8.3 src/Plugin/freelinking/External.php \Drupal\freelinking\Plugin\freelinking\External::getPageTitle()
Get the page title by fetching from the external URL.
Parameters
string $url: The URL to fetch.
Return value
string The page title to use.
Throws
\GuzzleHttp\Exception\RequestException
2 calls to External::getPageTitle()
- DrupalOrg::buildLink in src/
Plugin/ freelinking/ DrupalOrg.php - Build a link with the plugin.
- External::buildLink in src/
Plugin/ freelinking/ External.php - Build a link with the plugin.
File
- src/
Plugin/ freelinking/ External.php, line 150
Class
- External
- Freelinking external link plugin.
Namespace
Drupal\freelinking\Plugin\freelinkingCode
protected function getPageTitle($url) {
// Try to fetch the URL.
$response = $this->client
->get($url);
$body = $response
->getBody()
->getContents();
// Extract the page title from either the h1 or h2.
if (preg_match('/<h1.*>(.*)<\\/h1>/', $body, $matches)) {
if (strlen($matches[1]) < 3 && preg_match('/<h2.*>(.*)<\\/h2>/', $body, $h2_matches)) {
return $h2_matches[1];
}
return $matches[1];
}
return '';
}