Click to See Complete Forum and Search --> : storing url in MySql


Solaar
08-04-2006, 05:46 AM
Hello, I'm trying to store URLs in MySql but instead of the URL going in, all the html for the page the URL points to goes in instead, similarly if try and echo the URL the page is echoed instead. Have searched but cannot find an answer. Me code is below, the bottom part where $URL is the part i'm refering to. Anyone know what to do? .

Solaar

<?php

$database= "birds";
$user = "root";
$password = "";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

//get page contents
$page = file_get_contents("http://www.jagprops.co.uk");

//find urls in $page, matches are put in $matches
preg_match_all ("/href=[\"']?([^\"' >]+)/", $page, $matches);

$count = count($matches[0]);

for ( $counter = 0; $counter <= $count; $counter++){

$link = $matches[0][$counter];

$link = preg_replace( "/href=[\"']/","", $link);

$link = preg_replace( "/href=/","", $link);

if(!(($link[0] == 'h') || ($link[0] == 'H') || ($link[0] == '/'))) {

$link = '/'.$link;

echo "$link<br/>";

}

else{
echo "$link<br/>";
}

$URL = $page.$link;
mysql_query("INSERT INTO red (url) VALUES ('$URL') ")
or die(mysql_error());

}
?>

bennythemink
08-04-2006, 06:00 AM
hey, this prob is a stupid answer to this but why not put a string before the url address before you store into the database? some thing like:

$URL = "address_" . $URL;

then parse the $url when your writing it onscreen, (ie take off the string). not sure if this will work or give u a cant find page error.

Solaar
08-04-2006, 06:39 AM
tried that and all that happens is 'address_' gets appended to the beginning of all the html. It's a strange one, i think that the rest my code is to blame or something on my server needs switching off or on.

bennythemink
08-04-2006, 06:49 AM
heres two simple functions i created in a project that does what i suggested, it might save you some time in writing them yourself.



function chk_addU_MANUFACTURER($manufacturer)
{
$manufacturer = trim($manufacturer);
$manufacturer = strtoupper($manufacturer);

$instances = substr_count($manufacturer,'U_MANUFACTURER');
#chk how many instances of U_MANUFACTURER there is in the string.
if($instances > 1)
{
#if string contains more than one instance of U_MANUFACTURER then get rid of them.
for($counter = 0; $counter < $instances; $counter++)
{
$manufacturer = subU_MANUFACTURER($manufacturer);
}
}

$instances = substr_count($manufacturer,'U_MANUFACTURER');

if($instances < 1)
{
#add U_MANUFACTURER to the beginning of the string.
$manufacturer = addU_MANUFACTURER($manufacturer);
}

return $manufacturer;
}

#drops the "U_MANUFACTURER" from the start of the supplied string.
function subU_MANUFACTURER($text)
{
trim($text);
if(substr($text,0,15) == "U_MANUFACTURER_")
{
return substr($text,15,strlen($text));
}
}

Solaar
08-04-2006, 07:00 AM
thanks but i've just realised i've been a complete tit and the fact that $page = file_get_contents("http://www.jagprops.co.uk") does exactly i want it to do means $page.$link also does exactly what it's supposed to. I wasn't thinking.

NogDog
08-04-2006, 07:07 AM
I think you want to use $matches[1] instead of $matches[0]. (0 => entire string, 1 => first sub-pattern)

bennythemink
08-04-2006, 08:21 AM
thanks but i've just realised i've been a complete tit and the fact that $page = file_get_contents("http://www.jagprops.co.uk") does exactly i want it to do means $page.$link also does exactly what it's supposed to. I wasn't thinking.

lol, we've all done it and will all do it again sometime :D