How to read the HTML file SPECIFIC Code??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

How to read the HTML file SPECIFIC Code??

Post 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]
Flamie
Forum Contributor
Posts: 166
Joined: Mon Mar 01, 2004 3:19 pm

Post 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);
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Post 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.
eshban
Forum Contributor
Posts: 184
Joined: Mon Sep 05, 2005 1:38 am

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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)
Post Reply