Solved

OOPHP

Posted on 2007-04-08
7
171 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
ID: 18873200
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
ID: 18873288
<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
ID: 18873294
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 16

Author Comment

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

Accepted Solution

by:
Yuval_Shohat earned 500 total points
ID: 18873376
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
ID: 18873409
i also noticed where i was going wrong...

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

Expert Comment

by:Yuval_Shohat
ID: 18873452
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Cpanel file manager 8 42
hbo knew my windows software 4 63
Read a remote csv file from a https URL 8 49
PHP error function not working on AWS 10 75
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

828 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