Jump to content

Need comments about my function :)


Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/56689-need-comments-about-my-function/
Share on other sites

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.

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.