Solved

OOPHP

Posted on 2007-04-08
7
169 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP Healthcheck 2 85
phpmailer in WHILE loop - weird results 10 28
How do I put a session for a radio button? 4 14
datetime in sql 6 31
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.

895 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

15 Experts available now in Live!

Get 1:1 Help Now