Page 1 of 1

How to save image names while reading a file

Posted: Tue Dec 19, 2006 11:02 pm
by eshban
Hello,

I have read a file from PHP function.
I want to save all the images name in an array or session.

Like my HTML CODE is this

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<table width="80%" border="1" align="center">
  <tr>
    <td width="52%">Text 1 </td>
    <td width="48%">Text2</td>
  </tr>
  <tr>
    <td><img src="blog_header4.jpg" width="200" height="150" /></td>
    <td><img src="blog_header5.jpg" width="200" height="150" /></td>
  </tr>
</table>

</body>
</html>



How can i do this??Any idea

Posted: Tue Dec 19, 2006 11:27 pm
by John Cartwright

Code: Select all

preg_match_all('#<img src="([^"]+)#i', $source, $matches);

echo '<pre>';
print_r($matches);
Enjoy,

Posted: Tue Dec 19, 2006 11:47 pm
by eshban
which value i can put in

$source, $matches

please reply

Posted: Tue Dec 19, 2006 11:48 pm
by John Cartwright
$source is the html code, however you obtain it.. and $matches is what is outputted..

read the manual preg_match_all()

Code: Select all

<?php



$source = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>

<table width="80%" border="1" align="center">
  <tr>
    <td width="52%">Text 1 </td>
    <td width="48%">Text2</td>
  </tr>
  <tr>
    <td><img src="blog_header4.jpg" width="200" height="150" /></td>
    <td><img src="blog_header5.jpg" width="200" height="150" /></td>
  </tr>
</table>

</body>
</html>';

preg_match_all('#<img src="([^"]+)#i', $source, $matches);

echo '<pre>';
print_r($matches[1]);

?>
Outputs

Code: Select all

Array
(
    [0] => blog_header4.jpg
    [1] => blog_header5.jpg
)

Posted: Wed Dec 20, 2006 12:02 am
by eshban
thank you so much