Welcome Guest, Not a member yet? Register   Sign In
Get title and meta tags
#11

[eluser]felyx[/eluser]
Stupid me of course the regex was the problem and my blurry eyes hehe Smile Thank you for pointing me in the right direction!

I have also rewritten a couple of things as now the get_meta_tags() function will return all meta tags in an associative array.

Here is the final code:

Code:
function get_title_data($url)
    {
        $title = "";

        urlencode($url);

        $cont = @file_get_contents($url);

        if ($cont != FALSE) {

            $title_exists = preg_match( "/&lt;title&gt;(.*)<\/title>/i", $cont, $match );

            if ($title_exists)
            {

                $title = strip_tags(@$match[ 1 ]);

                return $title;

             } else
             {

                return FALSE;

             }

        } else {

            return FALSE;

        }
            
    }

    function get_meta_data($url)
    {
        $meta = array();

        urlencode($url);

        $cont = @file_get_contents($url);

        if ($cont != FALSE) {

        $meta_exists = trim(preg_match_all("/&lt;meta[^&gt;]+(name|http-equiv)=\"([^\"]*)\"[^>]+content=\"([^\"]*)\"[^>]*>/i", $cont, $out, PREG_PATTERN_ORDER));

        if ( ($meta_exists != FALSE) AND ($meta_exists > 0) )
        {
                
            for ($i=0;$i < count($out[2]);$i++) {
                $meta[strtolower($out[2][$i])] = $out[3][$i];
            }

            return $meta;
                
        } else {
                
            return FALSE;
                
        }

        } else {

            return FALSE;

        }
        
    }

Much cleaner, as short as it can be. Maybe someone else will find this usefull. Smile




Theme © iAndrew 2016 - Forum software by © MyBB