Solved

OOPHP

Posted on 2007-04-08
7
168 Views
Last Modified: 2011-10-03
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>';
?>
0
Comment
Question by:ellandrd
  • 4
  • 2
7 Comments
 
LVL 16

Expert Comment

by:HackneyCab
Comment Utility
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?
0
 
LVL 8

Expert Comment

by:Yuval_Shohat
Comment Utility
<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=-
0
 
LVL 8

Expert Comment

by:Yuval_Shohat
Comment Utility
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=-
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 16

Author Comment

by:ellandrd
Comment Utility
its still not working for me and ive copied your code.
0
 
LVL 8

Accepted Solution

by:
Yuval_Shohat earned 500 total points
Comment Utility
here is the code with my modification:
<?php
class ToolSet
{
      function createLink($url,$type)
      {
            $handle = $contents = $target = $description = $title = '';

            $target = (($type == 'external') ? ' onclick="javascript:window.open(this.href);return false;"' : '');

            ini_set('user_agent','MSIE 4\.0b2;');

            $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 $link;

echo '<p>'.$link.'</p>';
?>
it works fine.
remeber the results are not shown on the browser.
all that is output to the browser and is viewable by the user is:

Welcome - Robin Grady - SEAROBIN LTD - Offshore Construction and Subsea Intervention Specialist


right click the page and do "view sorce" for the code that is sent from the php script to the user's browser.
-=Yuval=-
0
 
LVL 16

Author Comment

by:ellandrd
Comment Utility
i also noticed where i was going wrong...

i had () after ToolSet in my Class...
0
 
LVL 8

Expert Comment

by:Yuval_Shohat
Comment Utility
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=-
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now