Solved

OOPHP

Posted on 2007-04-08
7
170 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

770 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