Jump to content

If Not Found Display Nothing...How?


mnybud

Recommended Posts

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);
?>

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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..
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.