The script of this tutorial takes the URL of a given page from which the page title is fetched.
To get started, just pass the url of the page that you wish to fetch its content to the getPage_Title_From_URL('url here') function and you are done.
code:
<?php
function getPage_Title_From_URL($pageURL)
{
$readPG = fopen($pageURL, 'r');
$x = 0;
$specifiedPG = '';
function getPage_Title_From_URL($pageURL)
{
$readPG = fopen($pageURL, 'r');
$x = 0;
$specifiedPG = '';
if ($readPG)
{
while (!feof($readPG) && ($x < 10))
{
$specifiedPG .= fread($readPG, 8192);
$x++;
}
fclose($readPG);
}
preg_match("/<title.*?>[\n\r\s]*(.*)[\n\r\s]*<\/title>/", $specifiedPG, $pageTitleFromURL);
if (isset($pageTitleFromURL[1]))
{
if ($pageTitleFromURL[1] == '')
{
return $pageURL;
}
$pageTitleFromURL = $pageTitleFromURL[1];
return trim($pageTitleFromURL);
}
else
{
return $pageURL;
}
}
//Example function call
echo getPage_Title_From_URL('https://www.dvlottery.state.gov/');
?>
{
while (!feof($readPG) && ($x < 10))
{
$specifiedPG .= fread($readPG, 8192);
$x++;
}
fclose($readPG);
}
preg_match("/<title.*?>[\n\r\s]*(.*)[\n\r\s]*<\/title>/", $specifiedPG, $pageTitleFromURL);
if (isset($pageTitleFromURL[1]))
{
if ($pageTitleFromURL[1] == '')
{
return $pageURL;
}
$pageTitleFromURL = $pageTitleFromURL[1];
return trim($pageTitleFromURL);
}
else
{
return $pageURL;
}
}
//Example function call
echo getPage_Title_From_URL('https://www.dvlottery.state.gov/');
?>
No comments:
Post a Comment