Save attributes in variable and then insert into database

katerina

150.***.***.***
1,449 days ago

Save attributes in variable and then insert into database

Hi ,
I have saved an url in variable $url.

I would like to make a regular expression that matches all image tags and then to seperate the attributes from every tag so as to insert into a database.

Could anyone helps me ??

I know how can I find the name of image (I write it below) , but I don't know how can I save the attributes of each image.

Code:


$url="http://www.google.com";;
$file = file_get_contents($url);

preg_match_all('/<img.*?src\s*=\s*["\'](.+?)["\']/im', $file, $images);

foreach($images[1] as $image)
{
echo $image.'<br>';
}



Thanks a lot

Bill

84.***.***.***
1,448 days ago
Hi Katerina and welcome.

Try this:

PHP code:


<?php

if (!@mysql_connect('localhost', 'USERNAME', 'PASSWORD'))
{
echo"Couldn't connect to MySQL: ".mysql_error();
exit;
}
elseif (!@mysql_select_db('DATABASE'))
{
echo"Couldn't select the database: ".mysql_error();
exit;
}

$url="http://www.google.com";;
$file = file_get_contents($url);

preg_match_all('/<img.*?src\s*=\s*["\'](.+?)["\']/im', $file, $images);

foreach($images[1] as $image)
{
echo $image.'...';

if (@mysql_query("INSERT INTO images (image) VALUES ('".$image."')"))
{
echo"Saved!";
}
else
{
echo"Failed! ".mysql_error();
}

echo"<br />\n";
}

mysql_close();

?>



You need to edit the MySQL connection login information and database name and then make an "images" table in that database that has got a field named "image".

katerina

150.***.***.***
1,447 days ago
It works perfect!! :) :)

Thanks a lot

Bill

84.***.***.***
1,446 days ago
No problem. :) Depending on how you are going to use this I have a few tips for improving it. For example to check the $image variable for any possible mysql injection monkey business, and a function to turn relative image URLs into their absolute forms. But again, depends on purpose.

katerina

150.***.***.***
1,446 days ago
Could I find the line of every tag in html code ?

and print it for example

line 1 : a.gif

Bill

84.***.***.***
1,445 days ago
I think you would have to break up the document according to line breaks and read and do preg_match on each line in a loop, because as far as I know regexp can't return that information along with the results.

katerina

150.***.***.***
1,375 days ago
I am trying something like this :

Code:

<?php


if (!@mysql_connect('localhost', 'root', ''))
{
echo "Error : ".mysql_error();
exit;
}
elseif (!@mysql_select_db('Diplwmatikh'))
{
echo "Error : ".mysql_error();
exit;
}

$url="http://www.e-gov.gr";

$lines = file($url);

foreach ($lines as $line_num => $file) {

if (preg_match_all ( '#(?:<img )([^>]*?)(?:/?>)#is', $file, $imgtags, PREG_PATTERN_ORDER )) {
$imgtags[1][0] .= 'linenumber="' .$line_num. '"'; // define attribute as linenumber
}

$imgcontents = array();

foreach ( (array) $imgtags[1] as $img ){

preg_match_all ( '#([a-zA-Z]*?)=[\'"]([^"\']*)["\']#i', $img, $attributes, PREG_PATTERN_ORDER );

$attrs = array();

foreach ( (array) $attributes[1] as $key => $attr ) {
$attrs[$attributes[1][$key]] = $attributes[2][$key];
}

$imgcontents[] = $attrs;
}

$msg = '';

foreach($imgcontents as $img => $key1) {

$id = $img;
$query = "INSERT INTO eikones (id) VALUES ('" .$id. "')";
mysql_query($query);

foreach($key1 as $imgs => $key2) {
if ($imgs == 'src') {
$img = array_reverse(split('/', $key2));
$key2 = $img[0];
$imageName = $key2;
}

$query = "UPDATE eikones SET " .$imgs. " = '" .$key2. "' WHERE id = '" .$id. "'";

if (mysql_query($query)) $msg .= '<br /><b>' .$imageName. '</b> -> saved in database';
}
}
echo $msg;
}


mysql_close();

?>



But
1. I take error Fatal error: Maximum execution time of 30 seconds exceeded in...

2. The linumber attribute does not work in my database. What is my fault ??

3. I am trying the site of microsoft and because the src of img is in the end , I cannot find any images.


Some assistance??? PLEASE It's important!!!!