-
-
Notifications
You must be signed in to change notification settings - Fork 1
Basic Examples
Rumen Damyanov edited this page Sep 22, 2025
·
3 revisions
This guide provides simple, easy-to-follow examples to help you get started with the PHP-SEO package quickly. Perfect for beginners or when you need a quick reference.
Install the package using Composer:
composer require rumenx/php-seo<?php
require_once 'vendor/autoload.php';
use Rumenx\PhpSeo\Config\SeoConfig;
use Rumenx\PhpSeo\SeoManager;
// 1. Create configuration (no AI for simplicity)
$config = new SeoConfig([
'ai' => ['enabled' => false],
'cache' => ['enabled' => false],
]);
// 2. Create SEO manager
$seoManager = new SeoManager($config);
// 3. Your HTML content
$content = '
<h1>How to Bake the Perfect Chocolate Chip Cookies</h1>
<p>Learn the secrets to making delicious, chewy chocolate chip cookies that everyone will love.</p>
<h2>Ingredients</h2>
<p>You will need flour, butter, sugar, eggs, and chocolate chips.</p>
<h2>Instructions</h2>
<p>Mix ingredients, form dough, and bake for 12 minutes at 350°F.</p>
';
// 4. Analyze content
$analysis = $seoManager->analyze($content);
// 5. Generate SEO elements
$title = $seoManager->generateTitle($analysis);
$description = $seoManager->generateDescription($analysis);
$keywords = $seoManager->generateKeywords($analysis);
// 6. Display results
echo "Title: " . $title . "\n";
echo "Description: " . $description . "\n";
echo "Keywords: " . implode(', ', $keywords) . "\n";Output:
Title: How to Bake the Perfect Chocolate Chip Cookies
Description: Learn the secrets to making delicious, chewy chocolate chip cookies that everyone will love. Get the best ingredients and baking instructions.
Keywords: chocolate chip cookies, baking, recipe, ingredients, instructions
<?php
// Generate SEO data (same as above)
$title = $seoManager->generateTitle($analysis);
$description = $seoManager->generateDescription($analysis);
$keywords = $seoManager->generateKeywords($analysis);
// Create complete HTML page
$html = "<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
<title>{$title}</title>
<meta name=\"description\" content=\"{$description}\">
<meta name=\"keywords\" content=\"" . implode(', ', $keywords) . "\">
</head>
<body>
{$content}
</body>
</html>";
echo $html;<?php
$blogPost = '
<h1>10 Essential Tips for Remote Work Success</h1>
<p>Working from home can be challenging. Here are proven strategies to stay productive and maintain work-life balance.</p>
<h2>Create a Dedicated Workspace</h2>
<p>Having a specific area for work helps separate professional and personal life.</p>
<h2>Establish a Routine</h2>
<p>Consistent daily schedules improve focus and productivity.</p>
<h2>Use the Right Tools</h2>
<p>Communication platforms and project management tools are essential for remote teams.</p>
<img src="remote-work.jpg" alt="Person working from home office">
<h2>Stay Connected</h2>
<p>Regular video calls help maintain team relationships and collaboration.</p>
';
$analysis = $seoManager->analyze($blogPost);
echo "Analysis Results:\n";
echo "Word Count: " . $analysis['word_count'] . "\n";
echo "Reading Time: " . $analysis['reading_time'] . " minutes\n";
echo "Number of Headings: " . count($analysis['headings']) . "\n";
echo "Images: " . $analysis['images']['total'] . "\n";
echo "SEO Score: " . $analysis['seo_score'] . "/100\n";
// Show heading structure
echo "\nHeading Structure:\n";
foreach ($analysis['headings'] as $heading) {
echo str_repeat(' ', $heading['level'] - 1) . "H{$heading['level']}: {$heading['text']}\n";
}Output:
Analysis Results:
Word Count: 87
Reading Time: 1 minutes
Number of Headings: 5
Images: 1
SEO Score: 75/100
Heading Structure:
H1: 10 Essential Tips for Remote Work Success
H2: Create a Dedicated Workspace
H2: Establish a Routine
H2: Use the Right Tools
H2: Stay Connected
<?php
$productPage = '
<h1>Professional Wireless Headphones - Model XH-2000</h1>
<p>Experience premium sound quality with our flagship wireless headphones featuring active noise cancellation and 30-hour battery life.</p>
<h2>Key Features</h2>
<ul>
<li>Active noise cancellation technology</li>
<li>30-hour battery life</li>
<li>Premium leather ear cushions</li>
<li>Bluetooth 5.0 connectivity</li>
<li>Quick charge: 5 minutes = 2 hours playback</li>
</ul>
<h2>Technical Specifications</h2>
<p>Driver size: 40mm | Frequency response: 20Hz-20kHz | Weight: 250g</p>
<h2>What\'s in the Box</h2>
<p>Headphones, carrying case, USB-C charging cable, 3.5mm audio cable, quick start guide.</p>
<h2>Customer Reviews</h2>
<p>Rated 4.8/5 stars by over 2,000 customers. "Best headphones I\'ve ever owned!" - Sarah M.</p>
';
$analysis = $seoManager->analyze($productPage);
// Generate product-specific SEO
$title = $seoManager->generateTitle($analysis, [
'max_length' => 60,
'include_brand' => true,
'brand_name' => 'AudioPro',
]);
$description = $seoManager->generateDescription($analysis, [
'max_length' => 160,
'style' => 'benefit-focused',
'call_to_action' => true,
]);
echo "Product SEO:\n";
echo "Title: {$title}\n";
echo "Description: {$description}\n";Output:
Product SEO:
Title: Professional Wireless Headphones XH-2000 - AudioPro
Description: Experience premium sound with XH-2000 wireless headphones. Active noise cancellation, 30-hour battery, Bluetooth 5.0. Order now for superior audio quality!
<?php
$content = '<h1>Best Pizza Recipes for Home Cooking</h1><p>Master the art of homemade pizza with these delicious and easy recipes.</p>';
$analysis = $seoManager->analyze($content);
// Different title styles
$basicTitle = $seoManager->generateTitle($analysis);
echo "Basic: {$basicTitle}\n";
$shortTitle = $seoManager->generateTitle($analysis, ['max_length' => 40]);
echo "Short: {$shortTitle}\n";
$brandedTitle = $seoManager->generateTitle($analysis, [
'max_length' => 60,
'include_brand' => true,
'brand_name' => 'FoodieHub',
]);
echo "Branded: {$brandedTitle}\n";
// Multiple variations
$variations = $seoManager->generateTitleVariations($analysis, ['count' => 3]);
echo "\nVariations:\n";
foreach ($variations as $i => $title) {
echo ($i + 1) . ". {$title}\n";
}Output:
Basic: Best Pizza Recipes for Home Cooking
Short: Best Pizza Recipes for Home
Branded: Best Pizza Recipes for Home Cooking - FoodieHub
Variations:
1. Best Pizza Recipes for Home Cooking
2. Homemade Pizza: Best Recipes for Home Cooking
3. Master Home Pizza Cooking with Best Recipes
<?php
$analysis = $seoManager->analyze($content);
// Different description styles
$basic = $seoManager->generateDescription($analysis);
echo "Basic: {$basic}\n\n";
$withCTA = $seoManager->generateDescription($analysis, [
'call_to_action' => true,
]);
echo "With CTA: {$withCTA}\n\n";
$benefitFocused = $seoManager->generateDescription($analysis, [
'style' => 'benefit-focused',
'max_length' => 140,
]);
echo "Benefit-focused: {$benefitFocused}\n\n";
$question = $seoManager->generateDescription($analysis, [
'style' => 'question',
]);
echo "Question style: {$question}\n";<?php
$analysis = $seoManager->analyze($content);
// Basic keywords
$basic = $seoManager->generateKeywords($analysis);
echo "Basic keywords: " . implode(', ', $basic) . "\n\n";
// Long-tail focus
$longTail = $seoManager->generateKeywords($analysis, [
'focus' => 'long-tail',
'max_keywords' => 15,
]);
echo "Long-tail: " . implode(', ', $longTail) . "\n\n";
// Short-tail focus
$shortTail = $seoManager->generateKeywords($analysis, [
'focus' => 'short-tail',
'max_keywords' => 8,
]);
echo "Short-tail: " . implode(', ', $shortTail) . "\n";<?php
class SimpleBlog
{
private $seoManager;
private $posts = [];
public function __construct($seoManager)
{
$this->seoManager = $seoManager;
}
public function addPost($title, $content, $slug = null)
{
$slug = $slug ?: $this->createSlug($title);
// Auto-generate SEO
$fullContent = "<h1>{$title}</h1>" . $content;
$analysis = $this->seoManager->analyze($fullContent);
$this->posts[$slug] = [
'title' => $title,
'content' => $content,
'slug' => $slug,
'meta_title' => $this->seoManager->generateTitle($analysis),
'meta_description' => $this->seoManager->generateDescription($analysis),
'keywords' => $this->seoManager->generateKeywords($analysis),
];
}
public function getPost($slug)
{
return $this->posts[$slug] ?? null;
}
public function renderPost($slug)
{
$post = $this->getPost($slug);
if (!$post) {
return '<h1>Post Not Found</h1>';
}
return "<!DOCTYPE html>
<html>
<head>
<title>{$post['meta_title']}</title>
<meta name=\"description\" content=\"{$post['meta_description']}\">
<meta name=\"keywords\" content=\"" . implode(', ', $post['keywords']) . "\">
</head>
<body>
<article>
<h1>{$post['title']}</h1>
{$post['content']}
</article>
</body>
</html>";
}
private function createSlug($title)
{
return strtolower(str_replace(' ', '-', preg_replace('/[^A-Za-z0-9 ]/', '', $title)));
}
}
// Usage
$blog = new SimpleBlog($seoManager);
$blog->addPost(
'Getting Started with PHP',
'<p>PHP is a powerful server-side scripting language perfect for web development.</p><p>This guide covers the basics of PHP programming.</p>'
);
echo $blog->renderPost('getting-started-with-php');<?php
class ProductCatalog
{
private $seoManager;
private $products = [];
public function __construct($seoManager)
{
$this->seoManager = $seoManager;
}
public function addProduct($name, $description, $price, $sku)
{
$content = "<h1>{$name}</h1><p>{$description}</p><p>Price: \${$price}</p>";
$analysis = $this->seoManager->analyze($content);
$this->products[$sku] = [
'name' => $name,
'description' => $description,
'price' => $price,
'sku' => $sku,
'meta_title' => $this->seoManager->generateTitle($analysis, [
'include_brand' => true,
'brand_name' => 'TechStore',
]),
'meta_description' => $this->seoManager->generateDescription($analysis, [
'call_to_action' => true,
]),
];
}
public function renderProduct($sku)
{
$product = $this->products[$sku] ?? null;
if (!$product) {
return '<h1>Product Not Found</h1>';
}
return "<!DOCTYPE html>
<html>
<head>
<title>{$product['meta_title']}</title>
<meta name=\"description\" content=\"{$product['meta_description']}\">
<!-- Product Schema -->
<script type=\"application/ld+json\">
{
\"@context\": \"https://schema.org\",
\"@type\": \"Product\",
\"name\": \"{$product['name']}\",
\"description\": \"{$product['description']}\",
\"sku\": \"{$product['sku']}\",
\"offers\": {
\"@type\": \"Offer\",
\"price\": \"{$product['price']}\",
\"priceCurrency\": \"USD\"
}
}
</script>
</head>
<body>
<h1>{$product['name']}</h1>
<p>{$product['description']}</p>
<div class=\"price\">\${$product['price']}</div>
<button>Add to Cart</button>
</body>
</html>";
}
}
// Usage
$catalog = new ProductCatalog($seoManager);
$catalog->addProduct(
'Wireless Mouse',
'Ergonomic wireless mouse with precision tracking and long battery life.',
29.99,
'MOUSE-001'
);
echo $catalog->renderProduct('MOUSE-001');<?php
function checkSeoScore($content, $seoManager)
{
$analysis = $seoManager->analyze($content);
echo "SEO Report\n";
echo "==========\n";
echo "Overall Score: {$analysis['seo_score']}/100\n\n";
// Content analysis
echo "Content Analysis:\n";
echo "- Word count: {$analysis['word_count']} " .
($analysis['word_count'] >= 300 ? "✓" : "⚠ (minimum 300 recommended)") . "\n";
echo "- Reading time: {$analysis['reading_time']} minutes\n";
echo "- Character count: {$analysis['char_count']}\n\n";
// Structure analysis
echo "Structure Analysis:\n";
echo "- Title tags (H1): " . count($analysis['title_tags']) .
(count($analysis['title_tags']) === 1 ? " ✓" : " ⚠ (should be exactly 1)") . "\n";
echo "- Total headings: " . count($analysis['headings']) . "\n";
echo "- Images: {$analysis['images']['total']}\n";
echo "- Images without alt text: {$analysis['images']['without_alt']} " .
($analysis['images']['without_alt'] === 0 ? "✓" : "⚠") . "\n";
echo "- Internal links: {$analysis['links']['internal']}\n";
echo "- External links: {$analysis['links']['external']}\n\n";
// Recommendations
if (!empty($analysis['recommendations'])) {
echo "Recommendations:\n";
foreach ($analysis['recommendations'] as $recommendation) {
echo "- {$recommendation}\n";
}
}
}
// Test your content
$testContent = '
<h1>Complete Guide to SEO Optimization</h1>
<p>Search Engine Optimization is crucial for online success.</p>
<h2>On-Page SEO</h2>
<p>Focus on content quality and keyword optimization.</p>
<img src="seo-chart.jpg" alt="SEO performance chart">
<h2>Technical SEO</h2>
<p>Ensure fast loading times and mobile responsiveness.</p>
<a href="https://example.com">External SEO resource</a>
';
checkSeoScore($testContent, $seoManager);Output:
SEO Report
==========
Overall Score: 78/100
Content Analysis:
- Word count: 45 ⚠ (minimum 300 recommended)
- Reading time: 1 minutes
- Character count: 287
Structure Analysis:
- Title tags (H1): 1 ✓
- Total headings: 3
- Images: 1
- Images without alt text: 0 ✓
- Internal links: 0
- External links: 1
Recommendations:
- Increase content length to at least 300 words
- Add more internal links to related content
- Consider adding more detailed descriptions
<?php
// Article content
$article = '
<h1>How to Start a Vegetable Garden</h1>
<p>Growing your own vegetables is rewarding and provides fresh, healthy food for your family.</p>
<h2>Choosing the Right Location</h2>
<p>Select a spot with 6-8 hours of direct sunlight daily.</p>
<h2>Preparing the Soil</h2>
<p>Test soil pH and add compost for optimal growing conditions.</p>
<h2>Selecting Vegetables</h2>
<p>Start with easy-to-grow varieties like tomatoes, lettuce, and carrots.</p>
';
$analysis = $seoManager->analyze($article);
echo "Blog Post SEO:\n";
echo "Title: " . $seoManager->generateTitle($analysis) . "\n";
echo "Description: " . $seoManager->generateDescription($analysis) . "\n";
echo "Keywords: " . implode(', ', $seoManager->generateKeywords($analysis)) . "\n";<?php
// Landing page content
$landingPage = '
<h1>Professional Web Design Services</h1>
<p>Transform your business with custom web design that converts visitors into customers.</p>
<h2>Our Services</h2>
<ul>
<li>Custom website design</li>
<li>Mobile-responsive development</li>
<li>SEO optimization</li>
<li>E-commerce solutions</li>
</ul>
<h2>Why Choose Us</h2>
<p>10+ years experience, 500+ satisfied clients, 24/7 support.</p>
<h2>Get Started Today</h2>
<p>Contact us for a free consultation and quote.</p>
';
$analysis = $seoManager->analyze($landingPage);
echo "Landing Page SEO:\n";
echo "Title: " . $seoManager->generateTitle($analysis, [
'include_brand' => true,
'brand_name' => 'WebCraft',
]) . "\n";
echo "Description: " . $seoManager->generateDescription($analysis, [
'call_to_action' => true,
'style' => 'benefit-focused',
]) . "\n";<?php
// Category page content
$categoryPage = '
<h1>Men\'s Running Shoes</h1>
<p>Discover our collection of high-performance running shoes designed for comfort and durability.</p>
<h2>Featured Brands</h2>
<p>Nike, Adidas, New Balance, ASICS, Brooks</p>
<h2>Shop by Type</h2>
<ul>
<li>Road running shoes</li>
<li>Trail running shoes</li>
<li>Marathon shoes</li>
<li>Cross-training shoes</li>
</ul>
<h2>Size Guide</h2>
<p>Find your perfect fit with our comprehensive sizing chart.</p>
';
$analysis = $seoManager->analyze($categoryPage);
echo "Category Page SEO:\n";
echo "Title: " . $seoManager->generateTitle($analysis, [
'include_brand' => true,
'brand_name' => 'SportShop',
]) . "\n";
echo "Description: " . $seoManager->generateDescription($analysis, [
'style' => 'feature-focused',
]) . "\n";- Minimum Word Count: Aim for at least 300 words for better SEO
- Clear Structure: Use proper heading hierarchy (H1, H2, H3)
- Alt Text: Always include alt text for images
- Internal Links: Link to related content on your site
- External Links: Link to authoritative external sources
<?php
// Good title examples
$goodTitles = [
'How to Bake Perfect Chocolate Chip Cookies',
'10 Essential Tips for Remote Work Success',
'Complete Guide to WordPress SEO in 2024',
'Best Running Shoes for Marathon Training',
];
// Generate optimized titles
foreach ($goodTitles as $title) {
$content = "<h1>{$title}</h1><p>Content about {$title}...</p>";
$analysis = $seoManager->analyze($content);
$optimized = $seoManager->generateTitle($analysis, ['max_length' => 60]);
echo "Original: {$title}\n";
echo "Optimized: {$optimized}\n\n";
}<?php
function generateOptimizedDescription($content, $seoManager)
{
$analysis = $seoManager->analyze($content);
return $seoManager->generateDescription($analysis, [
'max_length' => 155, // Leave room for search engines
'call_to_action' => true,
'include_keywords' => true,
]);
}
// Example usage
$content = '<h1>Healthy Breakfast Recipes</h1><p>Start your day right with nutritious and delicious breakfast options.</p>';
$description = generateOptimizedDescription($content, $seoManager);
echo "Optimized description: {$description}\n";
echo "Length: " . strlen($description) . " characters\n";Now that you've learned the basics, explore more advanced features:
- AI Integration - Add AI for smarter SEO generation
- Laravel Examples - Use with Laravel framework
- Symfony Examples - Use with Symfony framework
- Plain PHP Examples - Advanced plain PHP usage
- Configuration Guide - Detailed configuration options