Ninjakreborn Posted August 20, 2009 Share Posted August 20, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/171152-solved-get-dmoz-not-working-right/ Share on other sites More sharing options...
jonsjava Posted August 20, 2009 Share Posted August 20, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/171152-solved-get-dmoz-not-working-right/#findComment-902545 Share on other sites More sharing options...
Ninjakreborn Posted August 20, 2009 Author Share Posted August 20, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/171152-solved-get-dmoz-not-working-right/#findComment-902547 Share on other sites More sharing options...
Daniel0 Posted August 20, 2009 Share Posted August 20, 2009 I suppose you can do this: return strpos($string, 'No <b><a href="http://dmoz.org/">Open Directory Project</a></b> results found') !== false; Then it'll return false if there is no listing for that domain. Quote Link to comment https://forums.phpfreaks.com/topic/171152-solved-get-dmoz-not-working-right/#findComment-902548 Share on other sites More sharing options...
Ninjakreborn Posted August 20, 2009 Author Share Posted August 20, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/171152-solved-get-dmoz-not-working-right/#findComment-902552 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.