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
https://forums.phpfreaks.com/topic/19099-if-not-found-display-nothinghow/
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
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..
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
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.