Page 1 of 1

How to read the HTML file SPECIFIC Code??

Posted: Sat Dec 09, 2006 2:23 am
by eshban
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello, 

Iam  facing a small problem in PHP.

I have some html code like

[syntax="html"]
<!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">
  <tr>
    <td><img src="Image1.jpg" width="18" height="18" /></td>
    <td><img src="Image2.jpg" alt="sad" width="18" height="18" /></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>


I want to make a small script which reads all the contents of this HTML CODE under the "BODY" tag and replaces all the "src=Image1" with my path which i had already stored in a Variable. Like after the script completion the path will be "images\Cat\ABC\image1.jpg"

How can i do this. Plz help, because i tried it, but i am failed. Hope for a good response and suggestions

THanks


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sat Dec 09, 2006 2:53 am
by Flamie

Code: Select all

$file = file_get_contents("path to your html file here");
$file = explode("<body>", $file);
str_replace("src=image1", "src=".$yournewpath, $file[1]);
$file = implode("<body>", $file);
file_put_contents("path to your html file here", $file);

Posted: Sat Dec 09, 2006 2:56 am
by volka

Code: Select all

$pattern = '!<img src="([^"]+)"!';
$replace = '<img src="x/y/z/$1"';

$contents = file_get_contents('test.html');
echo preg_replace($pattern, $replace, $contents);
see http://en.wikipedia.org/wiki/Regular_Expression and http://de2.php.net/pcre

Posted: Mon Dec 11, 2006 7:11 am
by eshban
Thanks for the reply. But i am unable to use method 'file_get_contents'. It invokes PHP error like "Call to undefined function". I am using PHP 4.2.3

what can i do now.

Posted: Mon Dec 11, 2006 7:54 am
by eshban
Dear volka,

Your code is little bit confusing. It matches the string perfectly, but it is not write the new string to the file. So plz guide me that how can i replace string in physical file

Posted: Mon Dec 11, 2006 8:04 am
by volka
preg_replace returns a string. Instead of echoing it you have to save it to the file, e.g. with file_put_contents
see http://de3.php.net/file_put_contents (substitutions for older php versions are discussed in the user distributed notes)