Jump to content

[SOLVED] Get DMOZ not working right.


Ninjakreborn

Recommended Posts

My code:

function get_DMOZ($domain) {
  
  // Strip the http part if it's there
  if (strstr($domain, 'http://')) {
    $domain = substr($domain, 7);
  }
  
  // Strip the www part if it's there
  if (strstr($domain, 'www.')) {
    $domain = substr($domain, 4);
  }
  
  // Here is our pattern
  $pattern = '/^'.$domain.'/';

  // Get contents as string
  $string = file_get_contents('http://search.dmoz.org/cgi-bin/search?search='.$domain);

  // Match it
  preg_match($pattern, $string, $results);
  
  // Echo Match (This is for debugging)
  echo '<pre>';
  print_r($results);
  echo '</pre>';

  // Return results  
  return $results;
}

Sometimes it returns errors and the rest of the time it returns an empty array.

I want to use this to return if there were occurrences of the domain on DMOZ and

then could the DMOZ results..if the results are 1 or greater, then it's listed on

DMOZ and I can mark it as listed.

 

Is there anything wrong with the above code?

Link to comment
Share on other sites

<?php
function get_DMOZ($domain) {

// Strip the http part if it's there
$domain = str_ireplace("http://","",$domain);
$domain = str_ireplace("/","",$domain);
// Strip the www part if it's there
if (strstr($domain, 'www.')) {
	$domain = substr($domain, 4);
}

// Here is our pattern
$pattern = '/^'.$domain.'/';
// Get contents as string
$string = file_get_contents('http://search.dmoz.org/cgi-bin/search?search='.$domain);
// Match it
$results = preg_match($pattern, $string, $results); //I suck at regular expressions. This is as far as I can go at
// cleaning your script up. Sorry.
// Return results
return $results;
}
print get_DMOZ("http://jonsjava.com/");
?>

Above, I cleaned up some of your code, but I always get stuck at regular expressions. I'm hoping someone else can help you with that part.

Link to comment
Share on other sites

I am not sure I can use Ireplace on this. I think the server I am working on is PHP 4. That's why I am using the PHP 4 function.

 

I actually wasn't even familiar of that new function, thanks for pointing that out.

 

I changed the ^ because I found out that meant before in Regex but..that still only returns one result.

I need it to return more than one result....  If it's 1 result it's not in the DMOZ record because that

is showing up atleast once even when there are no results..when it shows up more than once it's considered

in the DMOZ record.

Link to comment
Share on other sites

I found out why.  preg_match returns only the first occurrence while preg_match_all returns all occurrences.  That was the mistake I made.

 

Thanks for the help.

 

I am not very experienced at Regular Expressions..I have stayed away from them until now..trying to get my head around them.

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.