Jump to content


Photo

If Not Found Display Nothing...How?


  • Please log in to reply
12 replies to this topic

#1 mnybud

mnybud
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 30 August 2006 - 05:03 AM

I have this Wordpress plugin code (shown below) that pul definitions from Google. I jut call it in my wordpress posts as ~GetDEF(Your_Search_Term)~ and it works great as long as Google has a definition for the term inputed. If Google does not have a definition then it just prints ~GetDEF(Your_Search_Term)~ instead which is kind of ugly.

I am wondering is there a way to make it so it displays nothing if the term is not found?
Here is the plugin code, its just one php file:

//Server Configuration
$host = "www.google.com";
$searchStr="http://www.google.co...tf8&q=define:";

function getDEF( $def ) {
    global $host,$searchStr,$path;
   
    $fp = fopen ($searchStr.$def, "r");
if (!$fp) {
  exit;
}
$contents ="";
$article="";
while (!feof ($fp)) {
  $contents .= fread($fp, 8192);
  }
  if (eregi ("<ul type=\"disc\">(.*)</ul>", $contents, $out)) {
      $article = $out[1];
  }
if (eregi ("<li>(.*)<li>", $contents, $out)) {
      $article = $out[1];
  }
$article=eregi_replace("<a(.*)</a>","",$article);
$article=strip_tags($article);

fclose($fp);
$article=trim($article);
if($article!=""){
$description=  "</br><span class=\"gdescription\"><br>This definition of ".$def.' is provided by the best search engine in the world, <a href="http://www.google.com">Google</a>!</span>';
$article = "<div class=\"Gdef\"><b>".$def.":<br></b>".$article.$description."</div>";
}
return $article;
}

function def_fy( $text ) {
    $text = preg_replace(
        "#\~GetDEF\((\S*)\)\~#imseU",
        "getDEF('$1')",
        $text
    );

return $text;
}

function def_css() {
    echo "
    <style type='text/css'>
    div.Gdef {
        border: 1px dashed silver;
        background-color: #f0f0f0;
padding:0 2em 0 2em;
    }
    .gdescription {
        font-size: 80%;
padding:0 2em 0 2em;
    }
    </style>
    ";
}


add_action('wp_head', 'def_css');
add_filter('the_content', 'def_fy', 2);
add_filter('the_excerpt', 'def_fy', 2);
?>



#2 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 30 August 2006 - 05:09 AM

I didn't read though all the code in detail, but are you printing the definition, or is it getting printed in a function that you don't have access to? If you are actually doing the printing, just do an if statement with a preg_match for something like "GetDEF" and not print if it finds it in the string.

regards
...drkstr

#3 radar

radar
  • Members
  • PipPipPip
  • Advanced Member
  • 645 posts
  • LocationSLC

Posted 30 August 2006 - 05:13 AM

well i didnt know which string is what in that.. but here would be the way to do it..

if ($whatever == "") {

} else {

output stuff here
}

that'd be the easy part..  I wasnt sure if it was $article as shown in GetDef function or $text as shown in def_fy function..

#4 mnybud

mnybud
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 30 August 2006 - 05:25 AM

yes I think the script is actually printing the definition...
how would I write the if and preg_match statement?

radar:
not sure where I would put that code...



sorry I am just learning.

#5 Satria Ox41464b

Satria Ox41464b
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 30 August 2006 - 05:28 AM

<?php
//if we got this "No definitions were found for" inside our $content
if (strpos($content, 'No definitions were found for')) echo 'cant find definition';
?>


#6 mnybud

mnybud
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 30 August 2006 - 05:32 AM

thanks but where do i put it?

#7 mnybud

mnybud
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 30 August 2006 - 05:43 AM

anyone?  ???

I have been fighting this script for too long and need to get some sleep....
If anyone can help I will say THANKS in the morning.......


remember I am a PHP newbie so please explain in detail if you can...thanks!

#8 drkstr

drkstr
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts
  • LocationSeattle, WA - USA

Posted 30 August 2006 - 02:31 PM

If I understand the code correctly, I think you should be able to add it to the def_fy function which appears to filter out text that is past to it. It would probably be the best place to put it.

function def_fy( $text ) {
  $text = preg_replace(
      "#\~GetDEF\((\S*)\)\~#imseU",
      "getDEF('$1')",
      $text  );

  if ( preg_match("/GetDEF/", $text) ) {
    $text = "Not found!";
  }


  return $text;
}

If this still does not do it, post the actual output on a undefined word (text in the browser, and the html source that gets printed). It will be esier to trace the program when you look at the output.

regards,
...drkstr

#9 mnybud

mnybud
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 30 August 2006 - 03:50 PM

thanks for the response but that does not work for me  :'(

This is all it prints in the html code when the term is not available:

<p>~GetDEF(some crap)~
</p>


I think it might be $article that displays the definition and not $text but I am not sure.

#10 mnybud

mnybud
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 30 August 2006 - 10:06 PM

still looking for a way to do this if anyone has any suggestions.

#11 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 30 August 2006 - 10:57 PM

It appears that your GetDEF() function returns the $article variable, so I would just change the end of the function to assign $article to some type of message indicating that no definition was found:


// ... THE FIRST PART OF THE GetDEF() function
// ...
// ...

$article=trim($article);

if($article!=""){
	$description=  "</br><span class=\"gdescription\">
	This definition of ".$def." is provided by the best search engine in the world, Google!</span>";

	$article = "<div class=\"Gdef\">".$def.":".$article.$description."</div>";
} else {
	$article = "No definitions for ".$def." were found!";
}

return $article;

}


... or you could set $article to be blank:

} else {
	$article = "";
}

return $article;

}


#12 mnybud

mnybud
  • Members
  • PipPipPip
  • Advanced Member
  • 80 posts

Posted 30 August 2006 - 11:21 PM

works great! Thanks!

#13 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 30 August 2006 - 11:37 PM

Glad to help!  ;)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users