PHP Help (Searching array then delete if found)

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
I have the following code to look through my iTunes Library and compile some data for me.

PHP:
<?php
$function = $_GET['function'];

$dbhost = 'localhost';
$dbusername = 'sterling';
$dbpasswd = '******;
$database_name = 'music';

$connection = mysql_pconnect("$dbhost","$dbusername","$dbpasswd")
	or die ("Couldn't connect to server.");

$db = mysql_select_db("$database_name", $connection)
	or die("Couldn't select database.");



$url1 = 'http://localhost';
$url2 = 'music';
$main_url = $url1.'/'.$url2;

$banned_urls = array("$url1", "$main_url", "$main_url.'/?C=D;O=A'", "$main_url.'/?C=M;O=A'", "$main_url.'/?C=S;O=A'", "$main_url.'/?C=D;O=A'", "$main_url.'/information.php'");

$links = GetInput("$main_url", '');

function GetInput($url) { 
   $handle=fopen($url, 'r');
   $contents = '';
   while (!feof($handle)) {
      $contents .= fread($handle, 8192);
   }
   fclose($handle);
   return $contents;
} 

function GetLinks($url, $filter) 
{
    $links = array();
    $match_domain='_[hH][tT][tT][pP]:\/\/(.*?)(/|$)_';
    preg_match($match_domain, $url, $res); 
    $domain=$res[1]; 
    if (!$domain) 
        return false; 
    if (!$input=GetInput($url)) 
        return false; 
    
    $lookfor='/<[aA]\s.*?[hH][rR][eE][fF]=[             "\']{0,}([-.,\%_\(\)|=~;+:\?\&\/a-zA-Z0-9]+)[ "\'>]/';
    preg_match_all($lookfor, $input, $data);
    while (list($k, $v)=each($data[1]))
    {
        // filter by
        if( strlen($filter) )
        {
            if( !strstr($v, $filter) )
            {
                continue;
            }    
        }
        if (stristr($v, 'javascript:'))
        { 
            // ignore - contains javascript
        } 
        elseif (stristr($v, '//')==$v) 
        { 
            $v='http:'.$v;
            $links[]=$v;
        } 
        elseif (stristr($v, 'http://')!=$v) 
        { 
            if (stristr($v, '/')!=$v)
                $sep='/';
            else
                $sep='';
            $v='http://'.$domain.$sep.$v; 
            $links[]=$v;
        } 
        else
            $links[]=$v;
    }
    if( count($links) )
    {
        $links=array_flip($links);
        $links=array_keys($links); 
    }
    else
        $links[] = 'No Data';
    return $links; 
} 

if (($function) == 'artists'){

$artists = GetLinks("$main_url", '');
$numb_of_artists = count($artists);

$query = mysql_query("UPDATE `information` SET artists='$numb_of_artists'") or die(mysql_error());


if (($query) == 'True'){
echo "Done. Also you have $numb_of_artists artists";
}else{
echo "Whoops, there was an error but, you have $numb_of_artists artists";
}

}elseif (($function) == 'albums'){

$links = GetInput("$main_url", '');
 
 $albums = GetLinks("$main_url", '');


  foreach ($albums as $artist_album) 
   $search = "%20";
   $replace = " ";
 $albums = preg_replace($search, $replace, $artist_album);
     $album_rar = GetLinks($artist_album);
       $rar_count = count($artist_rar);
	 $count = $count + $raw_count;


$query = mysql_query("UPDATE `information` SET albums='$count'") or die(mysql_error());

if (($query) == 'True'){
echo "Done. Also you have $count albums";
}else{
echo "Whoops, there was an error but, you have $count albums";
}

}elseif (($function) == 'songs'){
echo "not coded yet";
}else{
?>
Please choose an option<br>
<a href="information.php?function=artists">Artists</a>
<p><a href="information.php?function=albums">Albums</a>
<p><a href="information.php?function=songs">Songs</a>
<?php
}
?>

Basically what I am trying to do is search $albums and see if there are some links in there. If there are (i know there will be) delete them. The one important thing is that I atleast get rid of the http://localhost because that will make it search the music directory over and over.

Also, How do I remove %20 from the url. I tried

PHP:
   $search = "%20";
   $replace = " ";
 $albums = preg_replace($search, $replace, $artist_album);

but i get
Code:
Warning: preg_replace() [function.preg-replace]: No ending delimiter '%' found in G:\wamp\www\music\information.php on line 111

and line 111 is the $abbums = preg_replace..... code.

Anyways if you can help me with at least one of the problems that would be great.
 
Last edited:

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
Str_replace().. Haha.

I know you've been having problems with this for the last few days.. Is it finally working again?
 

Jake

Developer
Contributors
Messages
4,057
Reaction score
5
Points
0
hehe why would you use preg_replace()... :)
 
Top