Link to home
Start Free TrialLog in
Avatar of ellandrd
ellandrdFlag for Ireland

asked on

OOPHP

can you please check what is wrong with the code below.  I cannot get it to work.

<?php
class ToolSet()
{
      function createLink($url,$type)
      {
            $handle = $contents = $target = $description = $title = '';
            
            $target = (($type == 'external') ? ' onclick="javascript:window.open(this.href);return false;"' : '');
            
            $handle = fopen(trim($url),'r');
            
            $contents = fread($handle,700);
            
            fclose($handle);
      
            preg_match('/<title>(.*)<\/title>/i',$contents,$titles);
            
            preg_match('/<meta name="description" content="(.*)">/i',$contents,$descriptions);
            
            $title = $titles[1];
            
            $description = substr($descriptions[1],50);
            
            return '<a href="'.$url.'" title="'.trim($description).'"'.$target.'>'.trim($title).'</a>';
      }
}

$tools = new ToolSet();

$link = $tools->createLink('http://www.searobin.co.uk/','external');

echo '<p>'.$link.'</p>';
?>
Avatar of HackneyCab
HackneyCab
Flag of United Kingdom of Great Britain and Northern Ireland image

You may need to explain to the folk here what the above code is and isn't doing for you. What are you hoping for it to do, and what is it actually giving you?
Avatar of Yuval_Shohat
<p><a href="http://www.searobin.co.uk/" title="of my website" onclick="javascript:window.open(this.href);return false;">Welcome - Robin Grady - SEAROBIN LTD - Offshore Construction and Subsea Intervention Specialist</a></p>

is this the result you expect the script to output???
if so, there is one change you need to do:

this is your line:
preg_match('/<meta name="description" content="(.*)">/i',$contents,$descriptions);

this is what it should be:
preg_match('/<meta name="description" content="(.*)" \/>/i',$contents,$descriptions);

notice the \/ at the end of the maching string.

-=Yuval=-
o. one more thing,
your script initially gave me an error of not sendin header of the browser header, so i added
ini_set('user_agent','MSIE 4\.0b2;');
before the fopen command in order to fake some browser header id.

-=Yuval=-
Avatar of ellandrd

ASKER

its still not working for me and ive copied your code.
ASKER CERTIFIED SOLUTION
Avatar of Yuval_Shohat
Yuval_Shohat
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i also noticed where i was going wrong...

i had () after ToolSet in my Class...
looking at the perg match....
mybe it should be:
 preg_match('/<meta name="description" content="(.*)" *\/*>/i',$contents,$descriptions);

just to be on the safe side....

-=Yuval=-