سلام
من یه سوال داشتم می خواستم بدون با چه کد php می تونم title یه سایت رو که بهش می دم
برام برگردونه.:متفکر::متفکر::متفک ر::متفکر:
ممنون میشم کسی جوابمو بده.
ممنون.
Printable View
سلام
من یه سوال داشتم می خواستم بدون با چه کد php می تونم title یه سایت رو که بهش می دم
برام برگردونه.:متفکر::متفکر::متفک ر::متفکر:
ممنون میشم کسی جوابمو بده.
ممنون.
توی همین انجمن یه تاپیک بود بسرچ
با عبارت با قاعده میشه
باید اونها را پارس parse کنی.
اینها را ببین:
http://www.google.com/search?q=php+parse
http://www.kirupa.com/web/xml_php_parse_beginner.htm
معمولا عنوان صفحه بین تگ <title> قرار میگیره که به راحتی با عبارت منظم به صورت زیر می تونی بگیری
<?php
$url = 'http://example.com';
$c = curl_init ($url);
curl_setopt ($c, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec ($c);
curl_close ($c);
if( preg_match( '/<title>(.*?)<\/title>/si' , $page , $Match ) )
{
$title = $Match[1];
}
echo $title;
?>
اول سایت رو بریز داخل تابع file بعد با حلقه for خط به خط کد ها رو بخون هر زمان که سربرگ صفحه رسیدی break; کن
ساده ترین راه :
<form action="" method="GET">
<input type="text" name="site" value="http://" />
<input type="submit" />
</form>
<?php
if(isset($_GET['site'])){
preg_match("|<title>(.*?)</title>|s", file_get_contents($_GET['site']), $title);
print $title[1];
}
?>
خوب اینجوری هم باید کل فایل از سایت خونده شد
یک تابع کامل برای این کار
<?php
function getUrlData($url)
{
$result = false;
$contents = getUrlContents($url);
if (isset($contents) && is_string($contents))
{
$title = null;
$metaTags = null;
preg_match('/<title>([^>]*)<\/title>/si', $contents, $match );
if (isset($match) && is_array($match) && count($match) > 0)
{
$title = strip_tags($match[1]);
}
preg_match_all('/<[\s]*meta[\s]*name="?' . '([^>"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $contents, $match);
if (isset($match) && is_array($match) && count($match) == 3)
{
$originals = $match[0];
$names = $match[1];
$values = $match[2];
if (count($originals) == count($names) && count($names) == count($values))
{
$metaTags = array();
for ($i=0, $limiti=count($names); $i < $limiti; $i++)
{
$metaTags[$names[$i]] = array (
'html' => htmlentities($originals[$i]),
'value' => $values[$i]
);
}
}
}
$result = array (
'title' => $title,
'metaTags' => $metaTags
);
}
return $result;
}
function getUrlContents($url, $maximumRedirections = null, $currentRedirection = 0)
{
$result = false;
$contents = @file_get_contents($url);
// Check if we need to go somewhere else
if (isset($contents) && is_string($contents))
{
preg_match_all('/<[\s]*meta[\s]*http-equiv="?REFRESH"?' . '[\s]*content="?[0-9]*;[\s]*URL[\s]*=[\s]*([^>"]*)"?' . '[\s]*[\/]?[\s]*>/si', $contents, $match);
if (isset($match) && is_array($match) && count($match) == 2 && count($match[1]) == 1)
{
if (!isset($maximumRedirections) || $currentRedirection < $maximumRedirections)
{
return getUrlContents($match[1][0], $maximumRedirections, ++$currentRedirection);
}
$result = false;
}
else
{
$result = $contents;
}
}
return $contents;
}
?>