karthikeyan_coder Posted June 22, 2007 Share Posted June 22, 2007 I need to get urls from this page.. http://crappytools.com/pingurls/list.asp i am using my own function to get urls. tell me how is it.. If you have any idea/suggestion then plz let me know. <?php function line_stripper($url,$checker,$filter) { $file = file_get_contents($url); $string = explode("\n",$file); $j=0; for($i=1;$i<=count($string); $i++) { $res=1; $res = strpos($string[$i],$checker); if($res === 0) { $short = strpos($string[$i],$filter); if($short == "" ) { $list[$j] = $string[$i]; $j++; } } } return $list; } $result = line_stripper("http://crappytools.com/pingurls/list.asp","http","fails"); print_r($result); ?> $checker= "http" As we need urls alone then $filter=We dont need failed urls. So we filter from result urls. Quote Link to comment https://forums.phpfreaks.com/topic/56689-need-comments-about-my-function/ Share on other sites More sharing options...
wildteen88 Posted June 22, 2007 Share Posted June 22, 2007 Not too bad. You could just shorten it fully using regex. But for now you could just tidy the function up abit with using a foreach loop: <?php function line_stripper($url, $checker, $filter) { $file = file_get_contents($url); $lines = explode("\n", $file); $list = array(); foreach($lines as $line) { if( strpos($line, $checker) === 0 ) { $short = strpos($line, $filter); if( empty($short) ) { $list[] = trim(strip_tags($line)); } } } return $list; } $result = line_stripper('http://crappytools.com/pingurls/list.asp', 'http', 'fails'); echo '<pre>' . print_r($result, true) . '</pre>'; ?> I used trim and strip_tags function as you code was returning the <br> tag. ANd trim to just remove any whitespace characters at the beginning/end of the url. Quote Link to comment https://forums.phpfreaks.com/topic/56689-need-comments-about-my-function/#findComment-280269 Share on other sites More sharing options...
effigy Posted June 22, 2007 Share Posted June 22, 2007 file_get_contents + explode can be replaced with file. Also, I suspect you'll have "\r\n" as opposed to just "\n". Quote Link to comment https://forums.phpfreaks.com/topic/56689-need-comments-about-my-function/#findComment-280321 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.