[CLI]Urban Dictionary

Everything PHP-related! Discuss scripts, host challenges, request a script, or just ask for plain old help.

Moderators: IRC Operators, Support Team

rangerguy39
User
User
Posts: 397
Joined: Wed Jun 06, 2007 4:06 pm

[CLI]Urban Dictionary

Postby rangerguy39 » Fri Jan 11, 2008 10:31 pm

Code: Select all

<?PHP
fwrite(STDOUT, "Enter a word to look up: ");
$term = fgets(STDIN);

if(!$term) {
die('Error: Enter a word to look up!');


$s = file_get_contents("http://www.urbandictionary.com/define.php?term=".urlencode($term)."");
if(strpos($s, "isnt defined")) {
die("Definition not found");
}
else {
preg_match("/<p>(.*?)<\/p>/",$s, $match);
echo html_entity_decode(strip_tags($match[0]));
}

?>

only use it in the CLI
slushpuppy
User
User
Posts: 873
Joined: Sun Jul 16, 2006 2:23 pm

Postby slushpuppy » Sat Jan 12, 2008 12:58 pm

It's quite rare to get such scripts that are run locally, nice job ;)
-
User avatar
Dragon
IRC Operator
IRC Operator
Posts: 555
Joined: Mon Mar 26, 2007 7:35 pm

Postby Dragon » Thu Feb 14, 2008 1:17 am

does this work on http://localhost/ ?
cause it just gave me an error :?
Image
Zanith
IRC Operator
IRC Operator
Posts: 1082
Joined: Sun Nov 05, 2006 7:23 pm
Location: Internet
Contact:

Postby Zanith » Thu Feb 14, 2008 2:00 am

You will have to type at the command prompt

Code: Select all

php /path/to/script.php
http://www.scaryinter.net
Official SwiftIRC Slogon: <Ab`Couttsy> what topic
User avatar
Dragon
IRC Operator
IRC Operator
Posts: 555
Joined: Mon Mar 26, 2007 7:35 pm

Postby Dragon » Thu Feb 14, 2008 2:32 am

im still getting unexpected $end line 18 :?
Image
Zanith
IRC Operator
IRC Operator
Posts: 1082
Joined: Sun Nov 05, 2006 7:23 pm
Location: Internet
Contact:

Postby Zanith » Thu Feb 14, 2008 2:38 am

Untested but its missing a bracket.

Code: Select all

<?PHP
fwrite(STDOUT, "Enter a word to look up: ");
$term = fgets(STDIN);

if(!$term) {
die('Error: Enter a word to look up!');


$s = file_get_contents("http://www.urbandictionary.com/define.php?term=".urlencode($term)."");
if(strpos($s, "isnt defined")) {
die("Definition not found");
}
else {
preg_match("/<p>(.*?)<\/p>/",$s, $match);
echo html_entity_decode(strip_tags($match[0]));
}
}
?>
http://www.scaryinter.net
Official SwiftIRC Slogon: <Ab`Couttsy> what topic
User avatar
Dragon
IRC Operator
IRC Operator
Posts: 555
Joined: Mon Mar 26, 2007 7:35 pm

Postby Dragon » Thu Feb 14, 2008 2:42 am

it worked... more or less... it didnt return anything... but not returning any errors and its asking for a word to look up
:D
Image
rangerguy39
User
User
Posts: 397
Joined: Wed Jun 06, 2007 4:06 pm

Postby rangerguy39 » Mon Mar 17, 2008 11:47 pm

Zanith wrote:Untested but its missing a bracket.

Code: Select all

<?PHP
fwrite(STDOUT, "Enter a word to look up: ");
$term = fgets(STDIN);

if(!$term) {
die('Error: Enter a word to look up!');


$s = file_get_contents("http://www.urbandictionary.com/define.php?term=".urlencode($term)."");
if(strpos($s, "isnt defined")) {
die("Definition not found");
}
else {
preg_match("/<p>(.*?)<\/p>/",$s, $match);
echo html_entity_decode(strip_tags($match[0]));
}
}
?>
sorry, sometimes when I copy something, I miss the last line :?
Jonpopnycorn
User
User
Posts: 59
Joined: Sun May 21, 2006 11:38 pm
Location: Mars

Postby Jonpopnycorn » Sun Apr 27, 2008 2:17 am

Your code can't search, because it's only searching when $term is NULL, and when it's NULL it ends up using the die message.

Heres a working version

Code: Select all

<?PHP
   fwrite(STDOUT, "Enter a word to look up: ");
   $term = fgets(STDIN);
   if(!$term)
   {
      echo "Error: Enter a word to look up!";
   }
   else
   {
      $s = file_get_contents("http://www.urbandictionary.com/define.php?term=".urlencode($term)."");
      if(strpos($s, "isnt defined"))
      {
         echo "Definition not found";
      }
      else
      {
         preg_match("/<p>(.*?)<\/p>/",$s, $match);
         echo html_entity_decode(strip_tags($match[0]));
      }
   }
?>

Return to “PHP”

Who is online

Users browsing this forum: No registered users and 0 guests