Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,487 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,123 people online right now. Registration is fast and FREE... Join Now!




Problem with Multiple Word Definer

 
Reply to this topicStart new topic

Problem with Multiple Word Definer, Can't define more than one word

Moonbat
post 30 Jun, 2008 - 11:21 AM
Post #1


D.I.C Regular

Group Icon
Joined: 30 Jun, 2008
Posts: 384



Thanked 22 times

Dream Kudos: 600
My Contributions


Here is my full code.
CODE
<?php

/* If there is POST data */
if (isset($_POST['definetextarea'])) {
    /* Getting input and splitting it by word, counting elements in array */
    $theunexplodedwords = htmlentities($_POST['definetextarea']);
    $theexplodedwords = explode("\n", $theunexplodedwords);
    $fatso = count($theexplodedwords);
    
    /* Making a loop to define the words, getting the file contents of dictionary.com */
    for ($skinny=0;$skinny<$fatso;$skinny++) {
        $url = "http://dictionary.reference.com/browse/$theexplodedwords[$skinny]";
        $x = file_get_contents($url);
        
        /* Exploding the stuff before and after the first definition, in order to
           isolate the definition, proceeding to echo the definition */
        $y = explode("<table class=\"luna-Ent\"><tr><td valign=\"top\" class=\"dn\">1.</td><td valign=\"top\">", $x);
        $definition = explode(" </td></tr></table>", $y[1]);
        echo "$theexplodedwords[$skinny] - $definition[0] <br>";
    }
    
} else {

/* Echoing the HTML form if no POST data is detected */
echo <<<HTMLONE
<html>
<head><title>Multi-Word Definer</title></head>
<body>
<p align='center'>
Type in one word per line.
<br>
The Multi-Word Definer will define each word.
<br>
The definitions are from <a href="http://dictionary.reference.com/">Dictionary.com</a>.
<form name='defineform' action='$PHP_SELF' method='POST'>
<center><textarea name='definetextarea' rows='10' cols='20'>Enter words here</textarea></center>
<center><input type='submit' value='Define!'></center>
</form>
</p>
</body>
</html>
HTMLONE;
}
?>

My problem is that if I enter more than one word, it doesn't define all of them. If I enter 3 words, it'll skip the first 2 and define only the third one. I'd appreciate any help in fixing this problem biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

EvinOwen
post 1 Jul, 2008 - 03:49 PM
Post #2


New D.I.C Head

*
Joined: 1 Jul, 2008
Posts: 8



Thanked 1 times
My Contributions


So, I looked over your code, and I couldn't find anything wrong with it.

I think the problem lies with file_get_contents(), and I believe either its just resetting the file request or something. So, I tried the cUrl, which I figured would work better with this kind of thing anyway.

Also, I would suggest using a regular expression for this as well, instead of explode(). Mainly because not only should it be faster, but you can do it in one function call as well. Anyway, this is what I got.

CODE

<?php
if(isset($_POST['definetextarea'])){
$theunexplodedwords = htmlentities($_POST['definetextarea']);
$theexplodedwords = explode("\n", $theunexplodedwords);
$fatso = count($theexplodedwords);
ini_set('default_socket_timeout', 120);
// Open a new cUrl
$cURL = curl_init();
// Tell the cUrl to return the result instead of printing it!
curl_setopt($cURL,CURLOPT_RETURNTRANSFER,1);
for($skinny=0;$skinny<$fatso;$skinny++){
  unset($x);
   $url = "http://dictionary.reference.com/browse/$theexplodedwords[$skinny]";
   // Set the URL of the cUrl
   curl_setopt($cURL,CURLOPT_URL,$url);
   // Get the result of the call...
   $x = curl_exec($cURL);
   // Check and see if its empty
  if(!empty($x)){
   // Here is my regular expresion, basically it finds the location of the '1.' and copys whats after it
   preg_match("/1\.<\/td>(.*)<\/td>/U",$x,$res);
   // This strips the HTML tags off the result, so that we only see the text
   $definition = strip_tags($res[1]);
   echo "$theexplodedwords[$skinny] - $definition <br>"; } }
// Close the cUrl
curl_close($cURL); }
else{
echo <<<HTMLONE
<html>
<head><title>Multi-Word Definer</title></head>
<body>
<p align='center'>
Type in one word per line.
<br>
The Multi-Word Definer will define each word.
<br>
The definitions are from <a href="http://dictionary.reference.com/">Dictionary.com</a>.
<form name='defineform' action='$PHP_SELF' method='POST'>
<center><textarea name='definetextarea' rows='10' cols='20'>Enter words here</textarea></center>
<center><input type='submit' value='Define!'></center>
</form>
</p>
</body>
</html>
HTMLONE;
}
?>


Hope that works for you!
User is offlineProfile CardPM

Go to the top of the page

Moonbat
post 1 Jul, 2008 - 06:02 PM
Post #3


D.I.C Regular

Group Icon
Joined: 30 Jun, 2008
Posts: 384



Thanked 22 times

Dream Kudos: 600
My Contributions


Thanks for your help EvinOwen icon_up.gif

I'm currently testing all my scripts with WAMP, and I can't get cURL to work on it (I've never used cURL before but it looks cool). I followed the instructions from this tutorial.

http://www.h4x3d.com/libcurl-for-wamp-curl-for-wamp/

But I still can't get it working. I tried signing up for a free host and testing, but cURL wasn't enabled on it. I'll keep looking for a way to get cURL to work on WAMP.

But I want to know, is there any reason that file_get_contents is acting strangely? I can't understand why my code doesn't work even though the syntax is perfectly valid.

User is offlineProfile CardPM

Go to the top of the page

EvinOwen
post 1 Jul, 2008 - 06:45 PM
Post #4


New D.I.C Head

*
Joined: 1 Jul, 2008
Posts: 8



Thanked 1 times
My Contributions


QUOTE(Moonbat @ 1 Jul, 2008 - 06:02 PM) *

Thanks for your help EvinOwen icon_up.gif

I'm currently testing all my scripts with WAMP, and I can't get cURL to work on it (I've never used cURL before but it looks cool). I followed the instructions from this tutorial.

http://www.h4x3d.com/libcurl-for-wamp-curl-for-wamp/

But I still can't get it working. I tried signing up for a free host and testing, but cURL wasn't enabled on it. I'll keep looking for a way to get cURL to work on WAMP.

But I want to know, is there any reason that file_get_contents is acting strangely? I can't understand why my code doesn't work even though the syntax is perfectly valid.


hum, i thinking that might be a problem.

well, when i was running multiple definitions, i realized i was only getting the top of the page for the first few. so it wouldn't parse. i tried setting the timeout, but that didn't work either. though, i've never needed to loop through several file_get_contents statements before.

i dont have the time now, but ill look into it as soon as i can!
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 04:01PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month