Search code examples
phphtmlparsingpreg-matchtitle

Parse title from source code


i want to parse title from pages code source using title tag, someone made this for me but i dont know whats the problem because its not working, pls if anyone can help i really will appreciate that. thank you.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title></title>
<?php
$url; = '';
preg_match("#<title>(.*)</title>#Ui", file_get_contents($url), $title);
$title = substr($title[1],0,255);
echo "Title $url : ' $title '<br>";
?>
</head>
</html> 

Solution

  • You gotta fix this line: $url; = ''; to remove the semicolon after $url. Also, you should echo the result between the <title> and </title> tags, not after them. And lastly, a better structure for your application will be to execute PHP and then output HTML.

    <?php
        $url = ''; // <-- insert the URL of choice here, within quotes, starting with http://
        preg_match("#<title>(.*)</title>#Ui", file_get_contents($url), $title);
        $title = substr($title[1],0,255);
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
        <head>
            <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
            <title>
               <?php echo $title; ?>
            </title>
        </head>
    </html>