Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open a file, do a search and replace on text within it

Status
Not open for further replies.

madmax99

Programmer
Jul 12, 2002
1
US
I want to open a file and write to it changes after doing a search and replace on certain text.

this is how I am trying it, but it does not work.
any ideas?

$fp = fopen (&quot;<file_name>&quot;, &quot;r+&quot;);
$fputs_str = ereg_replace (&quot;sub_category_id&quot;, &quot;XXX&quot;, &quot;$fp&quot;);
fputs($fp, $fputs_str,strlen(100000));
 
fopen does not return a string containing the contents of the file. It returns an integer which is a handle to the file.
______________________________________________________________________
Why don't you click
on the cute little star below?
 
after fopen you should do

$string=fread($fp,filesize(&quot;file.txt&quot;));
then use the $string in the ereg.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
If you are opening a local file, why are you not using

$contents = @implode('',@file(<file>));
$contents = preg_replace(&quot;/sub_category_id/&quot;, &quot;XXX&quot;, &quot;$fp&quot;););

and do what you want with the modified content. ICQ: 54380631
online.dll
 
inlandpac,

Just one thing, file is not only for local files. It opens ftp and http calls to. It cannot opens HTTPS.

The advise to use fread instead of file, was by the fact that he needs to open the file, do something than re-write it. That way you can use a fread command. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top