Snippet Peek — Lightweight SEO Title + Description Checker
Snippet Peek is a lightweight, one-file PHP tool that pulls the <title> and <meta name="description"> from any public URL and shows the character counts. It’s built for quick “does this look right?” checks while you’re editing pages.
It’s intentionally simple: one PHP file, a basic form, and a small results box. No database, no frameworks, no extra pages.
How to use it
- Save the file below as
snippet-peek.phpanywhere on your site. - Visit it in your browser, paste a URL, click Preview.
- You’ll see the title + description and the character counts.
snippet-peek.php
<?php
declare(strict_types=1);
/**
* Snippet Peek (lightweight)
* - Fetch a URL
* - Extract <title> and <meta name="description">
* - Show character counts
*/
function h(string $s): string {
return htmlspecialchars($s, ENT_QUOTES, 'UTF-8');
}
function normalize_url(string $url): ?string {
$url = trim($url);
if ($url === '') return null;
// Require http(s) and a valid URL
if (!preg_match('~^https?://~i', $url)) return null;
if (!filter_var($url, FILTER_VALIDATE_URL)) return null;
return $url;
}
function fetch_html(string $url, int $timeout = 6, int $maxBytes = 220000): array {
if (function_exists('curl_init')) {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 4,
CURLOPT_CONNECTTIMEOUT => $timeout,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_USERAGENT => 'SnippetPeek/1.0',
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
]);
$body = curl_exec($ch);
$err = curl_error($ch);
$code = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
if ($body === false) return ['ok'=>false, 'error'=>"Fetch error: $err"];
if ($code >= 400) return ['ok'=>false, 'error'=>"HTTP $code"];
if (strlen($body) > $maxBytes) $body = substr($body, 0, $maxBytes);
return ['ok'=>true, 'body'=>$body];
}
$ctx = stream_context_create([
'http' => ['timeout' => $timeout, 'user_agent' => 'SnippetPeek/1.0']
]);
$body = @file_get_contents($url, false, $ctx);
if ($body === false) return ['ok'=>false, 'error'=>'Unable to fetch URL'];
if (strlen($body) > $maxBytes) $body = substr($body, 0, $maxBytes);
return ['ok'=>true, 'body'=>$body];
}
function parse_snippet(string $html): array {
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($html);
$title = '';
$desc = '';
$t = $dom->getElementsByTagName('title');
if ($t->length) {
$title = trim(preg_replace('/\s+/', ' ', $t->item(0)->textContent ?? ''));
}
foreach ($dom->getElementsByTagName('meta') as $meta) {
if (strtolower($meta->getAttribute('name')) === 'description') {
$desc = trim(preg_replace('/\s+/', ' ', $meta->getAttribute('content')));
break;
}
}
$tLen = function_exists('mb_strlen') ? mb_strlen($title, 'UTF-8') : strlen($title);
$dLen = function_exists('mb_strlen') ? mb_strlen($desc, 'UTF-8') : strlen($desc);
return [
'title' => $title,
'desc' => $desc,
'tLen' => (int)$tLen,
'dLen' => (int)$dLen,
];
}
$url_in = '';
$error = '';
$out = null;
if (!empty($_POST['url'])) {
$url_in = (string)$_POST['url'];
$url = normalize_url($url_in);
if (!$url) {
$error = 'Enter a valid http(s) URL.';
} else {
$f = fetch_html($url);
if (!$f['ok']) {
$error = $f['error'] ?? 'Fetch failed.';
} else {
$out = parse_snippet((string)$f['body']);
}
}
}
?>
<form method="post" style="max-width:720px;margin:18px auto;padding:14px;border:1px solid #ddd;border-radius:10px;font-family:Arial, sans-serif;">
<div style="font-size:18px;font-weight:bold;margin-bottom:8px;">Snippet Peek</div>
<div style="color:#555;margin-bottom:10px;">Paste a URL to preview its title + meta description.</div>
<div style="display:flex;gap:8px;flex-wrap:wrap;">
<input type="url" name="url" value="<?= h($url_in) ?>" placeholder="https://example.com" required
style="flex:1;min-width:240px;padding:10px;border:1px solid #ccc;border-radius:8px;">
<button type="submit" style="padding:10px 14px;border:1px solid #ccc;border-radius:8px;cursor:pointer;">Preview</button>
</div>
<div style="margin-top:8px;color:#777;font-size:12px;">
Tip: titles often look clean under ~60 chars; descriptions under ~160–170.
</div>
<?php if ($error): ?>
<div style="margin-top:10px;padding:10px;border:1px solid #e7b2b2;background:#fff1f1;border-radius:8px;">
<?= h($error) ?>
</div>
<?php endif; ?>
<?php if ($out): ?>
<div style="margin-top:12px;padding:12px;border:1px solid #d9ddff;background:#eef0ff;border-radius:10px;">
<div style="font-size:12px;color:#555;margin-bottom:4px;">Title (<?= (int)$out['tLen'] ?> chars)</div>
<div style="font-weight:bold;margin-bottom:10px;"><?= $out['title'] !== '' ? h($out['title']) : '<em>(none found)</em>' ?></div>
<div style="font-size:12px;color:#555;margin-bottom:4px;">Description (<?= (int)$out['dLen'] ?> chars)</div>
<div><?= $out['desc'] !== '' ? h($out['desc']) : '<em>(none found)</em>' ?></div>
</div>
<?php endif; ?>
</form>
Optional tweaks
- Restrict to your own site: only allow URLs that match your domain.
- Make it even smaller: remove the inline styling and keep plain HTML.
- Increase/decrease fetch size: change
$maxBytesinfetch_html().
Comments (0)
No comments yet — be the first.