Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

OOPHP

Posted on 2007-04-08
7
Medium Priority
?
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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 i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

670 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