Hi, I wrote the below code and I would like if anyone knows how can I insert all in a database. <?php $url="http://www.google.com"; $file = file_get_contents($url); preg_match_all ( '#(?:<img )([^>]*?)(?:/?>)#is', $file, $imgtags, PREG_PATTERN_ORDER ); $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; } var_dump ( $imgcontents ); ?> Code (markup): For example the out of this code is like that : array(57) { [0]=> array(5) { ["src"]=> string(66) "http://furious.adman.gr/banner?webspace=1946&dfl=yes&js=no" ["border"]=> string(1) "0" ["width"]=> string(1) "1" ["height"]=> string(1) "1" ["alt"]=> string(0) "" } [1]=> array(3) { ["src"]=> string(25) "/grfx/cNav/sports_sel.jpg" ["alt"]=> string(6) "sports" } And I would like to insert into database. For example ID | Src | border | width | length | alt Could someone help me? Thanks a lot
you can add this at the end of the code. <?php foreach($imgcontents as $img => $key1) { $id = $img; $query = "INSERT INTO tableImg (id) VALUES ('" .$id. "')"; mysql_query($query); foreach($key1 as $imgs => $key2) { $query = "UPDATE tableImg SET " .$imgs. " = '" .$key2. "' WHERE id = '" .$id. "'"; mysql_query($query); } } ?> PHP:
I would like to ask you and something else. First of all, I lose my first image with this code. Secondly, want to take only the name of image not all the path. Could you help me ? Thanks a lot for all!!!
I wanna start from the second one. with this code you can do it: <?php foreach($imgcontents as $img => $key1) { $id = $img; $query = "INSERT INTO tableImg (id) VALUES ('" .$id. "')"; mysql_query($query); foreach($key1 as $imgs => $key2) { if ($imgs == 'src') { $img = array_reverse(split('/', $key2)); $key2 = $img[0]; } $query = "UPDATE tableImg SET " .$imgs. " = '" .$key2. "' WHERE id = '" .$id. "'"; mysql_query($query); } } ?> PHP: For the first one, what do you mean by losing the first image? I tried the code using my site's url, and all of the images' name (with the first one) was inserted to db. Please, tell me if the problem occurs again