mnybud Posted August 30, 2006 Share Posted August 30, 2006 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.com/search?ie=utf8&oe=utf8&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);?> Quote Link to comment Share on other sites More sharing options...
drkstr Posted August 30, 2006 Share Posted August 30, 2006 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 Quote Link to comment Share on other sites More sharing options...
radar Posted August 30, 2006 Share Posted August 30, 2006 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.. Quote Link to comment Share on other sites More sharing options...
mnybud Posted August 30, 2006 Author Share Posted August 30, 2006 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. Quote Link to comment Share on other sites More sharing options...
Satria Ox41464b Posted August 30, 2006 Share Posted August 30, 2006 [code]<?php//if we got this "No definitions were found for" inside our $contentif (strpos($content, 'No definitions were found for')) echo 'cant find definition';?>[/code] Quote Link to comment Share on other sites More sharing options...
mnybud Posted August 30, 2006 Author Share Posted August 30, 2006 thanks but where do i put it? Quote Link to comment Share on other sites More sharing options...
mnybud Posted August 30, 2006 Author Share Posted August 30, 2006 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! Quote Link to comment Share on other sites More sharing options...
drkstr Posted August 30, 2006 Share Posted August 30, 2006 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 );[color=red] if ( preg_match("/GetDEF/", $text) ) { $text = "Not found!"; }[/color] 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 Quote Link to comment Share on other sites More sharing options...
mnybud Posted August 30, 2006 Author Share Posted August 30, 2006 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. Quote Link to comment Share on other sites More sharing options...
mnybud Posted August 30, 2006 Author Share Posted August 30, 2006 still looking for a way to do this if anyone has any suggestions. Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted August 30, 2006 Share Posted August 30, 2006 It appears that your [b]GetDEF()[/b] function returns the [b]$article[/b] variable, so I would just change the end of the function to assign [b]$article[/b] to some type of message indicating that no definition was found:[code=php:0]// ... 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;}[/code]... or you could set [b]$article[/b] to be blank:[code=php:0]} else { $article = "";}return $article;}[/code] Quote Link to comment Share on other sites More sharing options...
mnybud Posted August 30, 2006 Author Share Posted August 30, 2006 works great! Thanks! Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted August 30, 2006 Share Posted August 30, 2006 Glad to help! ;) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.