Created
February 4, 2019 21:11
-
-
Save cesarmiquel/2635f65a1a3bea5e42a1db5dbc800e78 to your computer and use it in GitHub Desktop.
Test <title> and <meta type="description"> tags for SEO purposes against a CSV or regexp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (count($argv) != 3) { | |
print("Usage: $argv[0] [domain] [csv file]\n\n"); | |
exit(0); | |
} | |
$domain = $argv[1]; | |
$csv_filename = $argv[2]; | |
if (substr($domain, 0, 4) != 'http') { | |
print("[domain] must be http url, for example: 'https://google.com'\n"); | |
exit(0); | |
} | |
if (!file_exists($csv_filename)) { | |
print("Can't find file $csv_filename!\n"); | |
exit(0); | |
} | |
# | |
# CSV Format: | |
# | |
# Url,Title,Description | |
# [relataive path],[title_regex],[desc_regex] | |
# .... | |
# | |
$csv = file_get_contents($csv_filename); | |
$lines = explode("\n", $csv); | |
$test_cases = []; | |
foreach($lines as $line) { | |
$fields = str_getcsv($line); | |
if (count($fields) == 3) { | |
$test_cases[] = $fields; | |
} | |
} | |
foreach($test_cases as $test_case) { | |
$url = trim($test_case[0]); | |
$title_regex = isset($test_case[1]) ? trim($test_case[1]) : ''; | |
$desc_regex = isset($test_case[2]) ? trim($test_case[2]) : ''; | |
// header! | |
if ($url == 'Url') { | |
continue; | |
} | |
test_page($domain . $url, $title_regex, $desc_regex); | |
} | |
function test_page($url, $title_regex, $desc_regex) { | |
libxml_use_internal_errors(true); | |
$doc = new DOMDocument(); | |
$doc->loadHTMLFile($url); | |
$title_value = get_title_tag_value($doc); | |
$metadata = get_meta_tags($url); | |
$desc_value = $metadata['description'] ? $metadata['description'] : ''; | |
// Log | |
log_info(); | |
log_info("Testing $url ..."); | |
$failed = FALSE; | |
// Test emmpty title | |
if (strlen(trim($title_value)) == 0) { | |
log_failure("<title> tag empty"); | |
$failed = TRUE; | |
} | |
// Compare with regex: | |
if (preg_match("/$title_regex/", $title_value) != 1) { | |
log_failure("<title> doesn't match. Expecting [$title_regex] found [$title_value]"); | |
$failed = TRUE; | |
} | |
else { | |
log_success("<title> matches value: [$title_value]"); | |
} | |
// Test emmpty description | |
if (strlen(trim($desc_value)) == 0) { | |
log_failure("description meta-tag empty."); | |
$failed = TRUE; | |
} | |
// Compare with regex: | |
if (preg_match("/$desc_regex/", $desc_value) != 1) { | |
log_failure("description meta-tag data doesn't match. Expecting [$desc_regex] found [$desc_value]"); | |
$failed = TRUE; | |
} | |
else { | |
log_success("description meta-tag matches value: [$desc_value]"); | |
} | |
return $failed; | |
} | |
function get_title_tag_value($doc) { | |
$count = 0; | |
foreach($doc->getElementsByTagName('title') as $title){ | |
$title_value = $title->nodeValue; | |
$count++; | |
} | |
if ($count != 1) { | |
return FALSE; | |
} | |
return $title_value; | |
} | |
function log_info($message = NULL) { | |
if ($message === NULL) { | |
print("\n"); | |
return; | |
} | |
print(">> \033[34m" . $message . "\033[0m\n" ); | |
} | |
function log_success($message) { | |
print(" [✔] " . $message . "\n" ); | |
} | |
function log_failure($message) { | |
print(" [✖] " . $message . "\n" ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment