Skip to content

Instantly share code, notes, and snippets.

@mokoshalb
Created December 14, 2017 08:02
Show Gist options
  • Save mokoshalb/2815a89586612c3533704447bbfd33af to your computer and use it in GitHub Desktop.
Save mokoshalb/2815a89586612c3533704447bbfd33af to your computer and use it in GitHub Desktop.
How to PHP regex match an HTML document's title
<?php
function parseTitle($html) {
// Get the text in a <title>
$matches = array();
preg_match("/<title>(.*)<\/title>/is", $html, $matches);
if (count($matches) > 1) {
return trim($matches[1]);
} else {
return null;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment