Edit files inside folders

Hi E's, in my site http://rdsrc.us/fCIeXk, I have lots of folders, and each folder represent one city. Inside of each folder I have on file that calls index.htm. In each index.htm I want to substitute some lines of code to others, in practice I want substitute the actual code of counter to the google analytic system.
In practice I have to erase the code between lines 211 to 223, and write the code of google analytics, that you can see in snippet code, before close </head>, and in this case the </head> it is in the line 24, so the line 23 is where I will put the code of google analytics, off course without erase the actual line 23.

I already have the solution to list all the folders, but I need help to edit all the files index.htm inside of each folder like I tell above:
echo "<pre>";

$dir = getcwd();

if (is_dir($dir))
    if ($dh = opendir($dir))
        while (($file = readdir($dh)) !== false)

How I edit every files index.htm?

The best regards, JC
Pedro ChagasWebmasterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Step One: Make a backup copy!
Pedro ChagasWebmasterAuthor Commented:
I've done!
Ray PaseurCommented:
This may be beside the point of your question, but I would like to offer up the suggestion anyway.

Why not take the time to refactor the application?  Instead of having numerous copies of the same script in many different folders, why not have one copy of the script in the web root folder?  You can give the script the name of the city in a GET argument, and it can translate the city name into a folder name, a data base name, etc.  This would make code maintenance MUCH easier with less risk of error.

Just a thought, ~Ray
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Hugh McCurdyCommented:
I'm with Ray on emphasizing doing a backup before starting said operation.  Me, I'd do at least 2.  One on the hard drive and another outside the computer (to CD, to USB key [memory stick], to another computer, whatever).

I would also do my testing on in a test area with fewer files (like 1 to start).

Otherwise, how to you identify the beginning of the text you want to change?  You mention you end at </head>.  Where do you start?

If you can identify where you start here's some psuedo code

loop to find files
    rename file to a backup copy;
    open the backup copy for reading;
    open the new copy for writing (using the original name);

    while reading from the old file
        process a line;
        if the line is the tag for the start
            keep reading lines until we get to the end tag;
            write the Google code;
            write the end tag </head>;

            while reading from the old file
                read line;
                write line;
            write line to the writable file.

This could be better written.  I'd probably use functions.  But this should give you the idea.
Pedro ChagasWebmasterAuthor Commented:
Hi Ray, the first mission is change the system of counter, after that I will do more changes, like put some social tools of Facebook, like button and share buttons. I have certain I will do some changes more.
Your idea is nice just for the problem of system of counter's, but I need to do some changes more.

Note: All the files index.htm was very identical, the only difference between them was the meta-tags, all the rest was the same.

Can you help me please, I need the first example for in the future I do all the changes I want.

The best regards, JC
Pedro ChagasWebmasterAuthor Commented:
Hi again, let's try to do this more simple, I'm not a php expert, but with some good examples I work that examples and make the changes I was.

In snippet code you can see two different index.htm, the old and the new one with the changes (I create the two files just for this question, maybe can be more simple to all).
The characteristics of the old file is:
the title was the default title
the picture don't have any link
exists a text link

The changes I do to the new file was:
I change the line title, give a new title
I erase the text link
The picture now have a link

Talking in lines I do:
edit line 5, changing the title.
erase line 9 and 10.
change the line 8, now the picture have a link

Maybe with the new scenario is more easy you give me a example?

Regards, JC

//OLD FILE index.htm
<!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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<p><img src="flower_original.jpg" width="250" height="250" /></p>
<p><a href="example.com">link</a></p>

//NEW FILE index.htm
<!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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Title</title>
<p><a href="example.com"><img src="flower_original.jpg" width="250" height="250" /></a></p>

Open in new window

Pedro ChagasWebmasterAuthor Commented:
Hi again, please forget my last post, I remember now for one thing, some post ago I tell the only difference between files was the meta tags, and that ones stay in the top of the file. So, back to the real situation.
In snipped code I post the real code of one of the index.htm files, and the only thing I want preserve was the firsts 24 lines (to </head>). I can erase the code inside lines between 25-293.
In this new scenario, I thing is more simple experts help me.
In practice the script have to do the erase of lines 25-293, and substitute that line with the new code I invented.

The best regards, JC
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<TITLE>Fotos da barragem de Odeleite </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<META name="Robots" content="all">
<META name="Language" content="pt">
<META name="Distribution" content="Global">
<META name="Rating" content="General">
<META name="Author" content="Pedro Chagas">
<META name="Copyright" content="PCSXXI 2005">
<META name="Keywords" content="fotos, portugal, fotografia digital, imagens, odeleite, arte digital, barragem, cameras digitais, fotos digitais, ferias, viagem, voos, turismo rural, rio, praias, ">
<META name="Description" content="Fotos da barragem de odeleite,fotos de Portugal, Base de dados de fotografia digital, a maior base de dados em fotografia digital sobre terras e sitios de Portugal. ">
<script language="JavaScript" type="text/JavaScript">
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();

<div id="Layer1" style="position:absolute; left:0; top:0; width:775; height:200; z-index:5"><img src="http://portugal.montranet.com/centralpt/foto_inicial.gif" alt="Portugal Colec&ccedil;&atilde;o S&eacute;culo XXI" width="775" height="200"></div>
<div id="Layer2" style="position:absolute; left:412px; top:127px; width:363; height:73; z-index:6"><img src="nome.gif" alt="Portugal" width="363" height="73"></div>
<div id="Layer3" style="position:absolute; left:18px; top:201px; width:738; height:90; z-index:30"> 
  <iframe src="http://portugal.montranet.com/adsensept/dinheiro.htm" name="conteudo" width="738" height="90" scrolling="no" border="0" frameborder="no"></iframe></div>
<div id="Layer4" style="position:absolute; left:182px; top:347px; width:412; height:549; z-index:7"><img src="1.jpg" alt="fotos Portugal" width="412" height="549"></div>
<div id="Layer5" style="position:absolute; left:0px; top:313px; width:147px; height:1615px; z-index:8; background-color: #FFFFCC; layer-background-color: #FFFFCC; border: 1px none #000000;"> 
  <div align="center">
    <p align="left"><font size="-2"><a href="/portugal/abrantes/index.htm" target="_blank"><font face="Verdana">Abrantes</font></a><font face="Verdana"><br>
      <a href="/portugal/alcoutim/index.htm" target="_blank">Alcoutim</a><br>
      <a href="/portugal/_aldeialuz/index.htm" target="_blank">Aldeia da Luz </a><br>
      <a href="/portugal/almeida/index.htm" target="_blank">Almeida </a><br>
      <a href="/portugal/alcacer/index.htm" target="_blank">Alcacer do sal</a><br>
      <a href="/portugal/cabanas/index.htm" target="_blank">Cabanas de Tavira</a><br>
      <a href="/portugal/cacelavelha/index.htm" target="_blank">Cacela Velha</a><br>
      <a href="/portugal/cachopo/index.htm" target="_blank">Cachopo</a><br>
      <a href="/portugal/caldasmonchique/index.htm" target="_blank">Caldas de Monchique</a><br>
      <a href="/portugal/_geres/index.htm" target="_blank">Caldas do Ger&ecirc;s </a><br>
      <a href="/portugal/castelovide/index.htm" target="_blank">Castelo de Vide</a><br>
      <a href="/portugal/castelorodrigo/index.htm" target="_blank">Castelo Rodrigo </a><br>
      <a href="/portugal/castromarim/index.htm" target="_blank">Castro Marim </a><br>
      <a href="/portugal/_chaves/index.htm" target="_blank">Chaves</a><br>
      <a href="/portugal/elvas/index.htm" target="_blank">Elvas</a><br>
      <a href="/portugal/estremoz/index.htm" target="_blank">Estremoz</a><br>
      <a href="/portugal/evora/index.htm" target="_blank">&Eacute;vora</a><br>
      <a href="/portugal/evoramonte/index.htm">&Eacute;voramonte</a><br>
      <a href="/portugal/freixo/index.htm">Freixo de Espada a Cintra</a><br>
      <a href="/portugal/guarda/index.htm" target="_blank">Guarda</a><br>
      <a href="/portugal/idanha/index.htm" target="_blank">Idanha-a-Velha </a><br>
      <a href="/portugal/lagos/index.htm" target="_blank">Lagos </a><br>
      <a href="/portugal/lisboa/index.htm" target="_blank">Lisboa </a><br>
      <a href="/portugal/lisboarevisitada/index.htm" target="_blank">Lisboa Revisitada </a><br>
      <a href="/portugal/marvao/index.htm" target="_blank">Marv&atilde;o</a><br>
      <a href="/portugal/monchique/index.htm" target="_blank">Monchique</a> <br>
      <a href="/portugal/miranda/index.htm" target="_blank">Miranda do Douro</a><br>
      <a href="/portugal/mogadouro/index.htm" target="_blank">Mogadouro</a><br>
      <a href="/portugal/monsanto/index.htm" target="_blank">Monsanto</a><br>
      <a href="/portugal/monsaraz/index.htm" target="_blank">Monsaraz</a> <br>
      <a href="/portugal/_montalegre/index.htm" target="_blank">Montalegre</a> <br>
      <a href="/portugal/moura/index.htm" target="_blank">Moura</a> <br>
      <a href="/portugal/odeleite/index.htm" target="_blank">Odeleite</a><br>
      <a href="/portugal/ouguela/index.htm" target="_blank">Ouguela</a><br>
      <a href="/portugal/penamacor/index.htm" target="_blank">Penamacor</a> <br>
      <a href="/portugal/penhagarcia/index.htm" target="_blank">Penha Garcia</a><br>
      <a href="/portugal/portalegre/index.htm" target="_blank">Portalegre</a><br>
      <a href="/portugal/portocovo/index.htm" target="_blank">Porto Covo</a><br>
      <a href="/portugal/redondo/index.htm" target="_blank">Redondo</a> <br>
      <a href="/portugal/seia/index.htm" target="_blank">Seia</a> <br>
      <a href="/portugal/serpa/index.htm" target="_blank">Serpa</a><br>
      <a href="/portugal/sines/index.htm" target="_blank">Sines</a> <br>
      <a href="/portugal/tavira/index.htm" target="_blank">Tavira</a><br>
      <a href="/portugal/vianaalentejo/index.htm" target="_blank">Viana do Alentejo</a><br>
      <a href="/portugal/_vianacastelo/index.htm" target="_blank">Viana do Castelo</a><br>
      <a href="/portugal/milfontes/index.htm" target="_blank">Vila Nova de Mil Fontes</a><br>
      <a href="/portugal/vicosa/index.htm" target="_blank">Vila Vi&ccedil;osa</a><br>
      <a href="/portugal/_vinhais/index.htm" target="_blank">Vinhais</a><br>
      <a href="/portugal/zambujeira/index.htm" target="_blank">Zambujeira do Mar</a></font></font><font size="-1" face="Tahoma"></font><font size="-1" face="Tahoma"><br>
    <p align="left">&nbsp;</p>
    <p align="left"><font size="-2" face="Verdana"><a href="/portugal/_basilicaluzia/index.htm" target="_blank">Basilica 
      de Santa Luzia </a><br>
      <a href="/portugal/casteloaljezur/index.htm" target="_blank">Castelo de Aljezur </a><br>
      <a href="/portugal/almourol/index.htm" target="_blank">Castelo de Almourol </a><br>
      <a href="/portugal/castelocampomaior/index.htm" target="_blank">Castelo de Campo Maior</a> 
      <a href="/portugal/castelomarim/index.htm" target="_blank">Castelo de Castro Marim</a> 
      <a href="/portugal/castelobeja/index.htm" target="_blank">Castelo de Beja </a><br>
      <a href="/portugal/_castelobraganca/index.htm" target="_blank">Castelo de Bragan&ccedil;a 
      <a href="/portugal/castelocelorico/index.htm" target="_blank">Castelo de Celorico da Beira</a> 
      <a href="/portugal/casteloestremoz/index.htm" target="_blank">Castelo de Estremoz </a><br>
      <a href="/portugal/casteloleiria/index.htm" target="_blank">Castelo de Leiria </a><br>
      <a href="/portugal/castelomertola/index.htm" target="_blank">Castelo de Mertola</a> <br>
      <a href="/portugal/castelomonsanto/index.htm" target="_blank">Castelo de Monsanto</a> <br>
      <a href="/portugal/_castelomontalegre/index.htm" target="_blank">Castelo de Montalegre</a> 
      <a href="/portugal/castelomourao/index.htm">Castelo de Mour&atilde;o </a><br>
      <a href="/portugal/castelopaderne/index.htm" target="_blank">Castelo de Paderne </a><br>
      <a href="/portugal/casteloportomos/index.htm" target="_blank">Castelo de Porto de M&oacute;s</a> 
      <a href="/portugal/saojorge/index.htm" target="_blank">Castelo de S&atilde;o Jorge 
      <a href="/portugal/castelosilves/index.htm" target="_blank">Castelo de Silves </a><br>
      <a href="/portugal/cristo/index.htm" target="_blank">Convento de Cristo</a> <br>
      <a href="/portugal/almendres/index.htm" target="_blank">Cromeleque de Almendres</a> 
      <a href="/portugal/fortalezasagres/index.htm" target="_blank">Fortaleza de Sagres </a><br>
      <a href="/portugal/pessegueiro/index.htm" target="_blank">Fortaleza do Pessegueiro</a> 
      <a href="/portugal/sebastiao/index.htm" target="_blank">Fortaleza de S&atilde;o Sebasti&atilde;o 
      (Castro Marim)</a> <br>
      <a href="/portugal/_mealha/index.htm" target="_blank">Monumentos Megal&iacute;ticos 
      de Mealha (Cachopo)</a> <br>
      <a href="/portugal/batalha/index.htm" target="_blank">Mosteiro da Batalha </a><br>
      <a href="/portugal/pena/index.htm" target="_blank">Palacio da Pena</a> <br>
      <a href="/portugal/milreu/index.htm" target="_blank">Ru&iacute;nas de Milreu </a><br>
      <a href="/portugal/santuariofatima/index.htm" target="_blank">Santu&aacute;rio de F&aacute;tima</a></font></p>
    <p align="left"><font face="Verdana"><br>
    <p align="left">&nbsp;</p>
    <p align="left"><font face="Verdana"><font size="-2"><a href="/portugal/janelas/index.htm" target="_blank">Janelas 
      e portas de Almeida</a> </font></font></p>
    <p align="left">&nbsp;</p>
    <p align="left">&nbsp;</p>
    <p align="left"><font size="-2" face="Verdana"><a href="/portugal/pao/index.htm" target="_blank">Fazer 
      o P&atilde;o em S&atilde;o Marcos da Serra (Algarve)</a></font></p>
    <p align="left">&nbsp;</p>
    <p align="left">&nbsp;</p>
    <p align="left"><font size="-2" face="Verdana"><a href="/portugal/pulolobo/index.htm" target="_blank">Pulo 
      do Lobo (Mertola)</a><br>
      <a href="/portugal/riaformosa/index.htm" target="_blank">Ria Formosa (Algarve) </a><br>
      <a href="/portugal/serraestrela/index.htm" target="_blank">Serra da Estrela </a><br>
      <a href="/portugal/serratavira/index.htm" target="_blank">Serra de Tavira </a></font> 
<div id="Layer6" style="position:absolute; left:0; top:292px; width:381px; height:20px; z-index:1; background-color: #FEFAE2; layer-background-color: #FEFAE2;"> 
  <div align="center"><img src="http://portugal.montranet.com/centralpt/portugal.gif" alt="Portugal" width="30" height="20"> 
    <a href="indexuk.htm"><img src="http://portugal.montranet.com/centralpt/inglaterra.gif" alt="Inglaterra" width="30" height="20" border="0"></a> 
    <a href="indexes.htm"><img src="http://portugal.montranet.com/centralpt/espanha.gif" alt="Espanha" width="30" height="20" border="0"></a> 
    <a href="indexfr.htm"><img src="http://portugal.montranet.com/centralpt/franca.gif" alt="Fran&ccedil;a" width="30" height="20" border="0"></a> 
    <a href="indexal.htm"><img src="http://portugal.montranet.com/centralpt/alemanha.gif" alt="Alemanha" width="30" height="20" border="0"></a> 
    <a href="indexho.htm"><img src="http://portugal.montranet.com/centralpt/holanda.gif" alt="Holanda" width="30" height="20" border="0"></a> 
    <a href="indexit.htm"><img src="http://portugal.montranet.com/centralpt/italia.gif" alt="Italia" width="30" height="20" border="0"></a> 
    <a href="indexgr.htm"><img src="http://portugal.montranet.com/centralpt/grecia.gif" alt="Grecia" width="30" height="20" border="0"></a> 
    <a href="indexru.htm"><img src="http://portugal.montranet.com/centralpt/russia.gif" alt="Russia" width="30" height="20" border="0"></a> 
    <a href="indexch.htm"><img src="http://portugal.montranet.com/centralpt/china.gif" alt="China" width="30" height="20" border="0"></a> 
    <a href="indexjp.htm"><img src="http://portugal.montranet.com/centralpt/japao.gif" alt="Japao" width="30" height="20" border="0"></a> 
<div id="Layer7" style="position:absolute; left:383px; top:292px; width:392px; height:20px; z-index:9; background-color: #FFFFCC; layer-background-color: #FFFFCC; border: 1px none #000000;"> 
  <div align="center"><a href="http://www.portugal.montranet.com/" target="_blank"><font color="#006699" size="2" face="Verdana">direct&oacute;rio</font></a> 
    <font size="2" face="Verdana"><a href="http://www.portugal.montranet.com/preservar.htm#newsletter" target="_blank"><font color="#006699">newsletter</font></a> 
    <a href="http://www.portugal.montranet.com/pcsxxi.exe"><font color="#006699">screensaver</font></a></font></div>
<div id="Layer10" style="position:absolute; left:202px; top:1076px; width:130; height:173; z-index:11"><a href="pt4.htm" target="_blank"><img src="p4.jpg" alt="fotos Portugal" width="130" height="173" border="0"></a></div>
<div id="Layer11" style="position:absolute; left:441px; top:1076px; width:130; height:173; z-index:12"><a href="pt9.htm" target="_blank"><img src="p9.jpg" alt="fotos Portugal" width="130" height="173" border="0"></a></div>
<div id="Layer8" style="position:absolute; left:178px; top:1260px; width:173; height:130; z-index:13"><a href="pt2.htm" target="_blank"><img src="p2.jpg" alt="fotos Portugal" width="173" height="130" border="0"></a></div>
<div id="Layer12" style="position:absolute; left:420px; top:1260px; width:173; height:130; z-index:14"><a href="pt3.htm" target="_blank"><img src="p3.jpg" alt="fotos Portugal" width="173" height="130" border="0"></a></div>
<div id="Layer13" style="position:absolute; left:178px; top:1401px; width:173; height:130; z-index:15"><a href="pt5.htm" target="_blank"><img src="p5.jpg" alt="fotos Portugal" width="173" height="130" border="0"></a></div>
<div id="Layer14" style="position:absolute; left:420; top:1401; width:173; height:130; z-index:16"><a href="pt6.htm" target="_blank"><img src="p6.jpg" alt="fotos Portugal" width="173" height="130" border="0"></a></div>
<div id="Layer15" style="position:absolute; left:179px; top:1541px; width:173; height:130; z-index:17"><a href="pt7.htm" target="_blank"><img src="p7.jpg" alt="fotos Portugal" width="173" height="130" border="0"></a></div>
<div id="Layer16" style="position:absolute; left:420; top:1541; width:173; height:130; z-index:18"><a href="pt8.htm" target="_blank"><img src="p8.jpg" alt="fotos Portugal" width="173" height="130" border="0"></a></div>
<div id="Layer17" style="position:absolute; left:148px; top:313px; width:479px; height:634px; z-index:2;"></div>
<div id="Layer18" style="position:absolute; left:628px; top:313px; width:147px; height:375px; z-index:19; background-color: #FFFFCC; layer-background-color: #FFFFCC; border: 1px none #000000;"> 
  <p><font size="-2" face="Verdana"><a href="/portugal/alqueva/index.htm" target="_blank">Barragem 
    do alqueva </a><br>
    <a href="/portugal/_construcao/index.htm" target="_blank">Barragem do alqueva em constru&ccedil;&atilde;o</a> 
    <a href="/portugal/beliche/index.htm" target="_blank">Barragem do Beliche </a><br>
    <a href="/portugal/barragembode/index.htm" target="_blank">Barragem do Castelo de Bode 
    <a href="/portugal/fonteserne/index.htm" target="_blank">Barragem de Fonte de Serne</a> 
    <a href="/portugal/fratel/index.htm" target="_blank">Barragem do Fratel </a><br>
    <a href="/portugal/barragemodeleite/index.htm" target="_blank">Barragem de Odeleite </a><br>
    <a href="/portugal/santaclara/index.htm" target="_blank">Barragem de Santa Clara </a><br>
    <a href="/portugal/farolsagres/index.htm" target="_blank">Farol de Sagres </a></font></p>
  <p><font size="-2" face="Verdana"><a href="/portugal/barcos/index.htm" target="_blank">Barcos 
    de pesca </a><br>
    <a href="/portugal/madrugada/index.htm" target="_blank">Madrugada no mar</a> <br>
    <a href="/portugal/saude/index.htm" target="_blank">Senhora da Saude (Tavira)</a> 
    <a href="/portugal/traineiras/index.htm" target="_blank">Traineiras de pesca</a></font></p>
<div id="Layer19" style="position:absolute; left:0px; top:314px; width:147px; height:32px; z-index:20"><img src="http://portugal.montranet.com/centralpt/cidades.gif" alt="Cidades Vilas e Aldeias de Portugal" width="147" height="32"></div>
<div id="Layer20" style="position:absolute; left:0px; top:957px; width:147; height:32; z-index:21"><img src="http://portugal.montranet.com/centralpt/castelos.gif" alt="Castelos e Monumentos de Portugal" width="147" height="32"></div>
<div id="Layer21" style="position:absolute; left:0px; top:1399px; width:147; height:32; z-index:22"><img src="http://portugal.montranet.com/centralpt/pormenor.gif" alt="Pormenores de Portugal" width="147" height="32"></div>
<div id="Layer22" style="position:absolute; left:0px; top:1522px; width:147; height:32; z-index:23"><img src="http://portugal.montranet.com/centralpt/maxima.gif" alt="Nota Maxima" width="147" height="32"></div>
<div id="Layer23" style="position:absolute; left:0; top:1635px; width:147; height:32; z-index:24"><img src="http://portugal.montranet.com/centralpt/naturais.gif" alt="Serras e Zonas Naturais de Portugal" width="147" height="32"></div>
<div id="Layer24" style="position:absolute; left:628px; top:314px; width:147; height:32; z-index:25"><img src="http://portugal.montranet.com/centralpt/barragens.gif" alt="Barragens Praias e Farois de Portugal" width="147" height="32"></div>
<div id="Layer25" style="position:absolute; left:628px; top:549px; width:147; height:32; z-index:26"><img src="http://portugal.montranet.com/centralpt/especiais.gif" alt="Edi&ccedil;&otilde;es especiais sobre Portugal" width="147" height="32"></div>
<div id="Layer27" style="position:absolute; left:150px; top:1682px; width:616px; height:22px; z-index:28"> 
  <div align="center"><font size="-4">Todas as fotos aqui publicadas pertencem 
    &aacute; Colec&ccedil;&atilde;o Portugal S&eacute;culo XXI, &eacute; expressamente 
    proibido a c&oacute;pia destas mesmas fotos sem autoriza&ccedil;&atilde;o 
    previa do autor. Todo as fotografias aqui representadas s&atilde;o um exclusivo 
    Portugal Colec&ccedil;&atilde;o S&eacute;culo XXI, com o unico objectivo de 
    dar a conhecer partes de Portugal. </font></div>
<div id="Layer28" style="position:absolute; left:0px; top:2020px; width:20; height:20; z-index:29"><a target="_top" href="http://t.extreme-dm.com/?login=pcsxxi"> 
  <img src="http://t1.extreme-dm.com/i.gif" height=38
border=0 width=41 alt=""></a> 
  <script language="javascript1.2"><!--
</script><script language="javascript"><!--
EXd.write("<img src=\"http://t0.extreme-dm.com",
"l="+escape(EXd.referrer)+"\" height=1 width=1>");//-->
</script><noscript><img height=1 width=1 alt=""
<div id="Layer26" style="position:absolute; left:628px; top:688px; width:147px; height:376px; z-index:31; background-color: #FFFFCC; layer-background-color: #FFFFCC; border: 1px none #000000;"><iframe src="http://portugal.montranet.com/centralpt/novidades.htm" name="conteudo" width="147" height="376" scrolling="no" border="0" frameborder="no"></iframe></div>
<div id="Layer9" style="position:absolute; left:148px; top:930px; width:479px; height:116px; z-index:32"><a href="http://portugal.montranet.com/centralpt/publicidade.htm" target="_blank"><img src="http://portugal.montranet.com/centralpt/pubpt.gif" width="479" height="116" border="0"></a></div>
<div id="Layer29" style="position:absolute; left:644px; top:1064px; width:120; height:600; z-index:33; background-color: #FFFFCC; layer-background-color: #FFFFCC; border: 1px none #000000;"> 
  <script type="text/javascript"><!--
google_ad_client = "pub-7950738589222540";
google_ad_width = 120;
google_ad_height = 600;
google_language = 'pt';
google_ad_format = "120x600_as";
google_ad_type = "text_image";
google_ad_channel ="0910825892";
google_color_border = "FFFFFF";
google_color_bg = "FFFFCC";
google_color_link = "CC0000";
google_color_url = "000000";
google_color_text = "000000";
<script type="text/javascript"
<div id="Layer30" style="position:absolute; left:152px; top:1722px; width:612px; height:64px; z-index:34"> 
  <div align="center"> 
    <form method="get" action="http://www.google.pt/custom" target="google_window">
<table border="0" bgcolor="#ffffff">
<tr><td nowrap="nowrap" valign="top" align="left" height="32">
<a href="http://www.google.com/">
<img src="http://www.google.com/logos/Logo_25wht.gif"
border="0" alt="Google"></img></a>
<td nowrap="nowrap">
<input type="hidden" name="domains" value="www.portugal.montranet.com"></input>
<input type="text" name="q" size="31" maxlength="255" value=""></input>
<input type="submit" name="sa" value="Pesquisar"></input>
<td nowrap="nowrap">
<input type="radio" name="sitesearch" value=""></input>
<font size="-1" color="#000000">Web</font>
<input type="radio" name="sitesearch" value="www.portugal.montranet.com" checked="checked"></input>
<font size="-1" color="#000000">www.portugal.montranet.com</font>
<input type="hidden" name="client" value="pub-7950738589222540"></input>
<input type="hidden" name="forid" value="1"></input>
<input type="hidden" name="ie" value="ISO-8859-1"></input>
<input type="hidden" name="oe" value="ISO-8859-1"></input>
<input type="hidden" name="cof" value="GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;FORID:1;"></input>
<input type="hidden" name="hl" value="pt"></input>

<div id="Layer31" style="position:absolute; left:763px; top:1064px; width:12px; height:600; z-index:35; background-color: #FFFFCC; layer-background-color: #FFFFCC; border: 1px none #000000;"></div>
<div id="Layer32" style="position:absolute; left:628px; top:1064px; width:18px; height:600px; z-index:36; background-color: #FFFFCC; layer-background-color: #FFFFCC; border: 1px none #000000;"></div>
<div id="Layer33" style="position:absolute; left:152px; top:1830px; width:612px; height:98px; z-index:37"> 
  <iframe src="http://portugal.montranet.com/centralpt/ultimaparte.htm" name="conteudo" width="612" height="98" scrolling="no" border="0" frameborder="no"></iframe></div>
<div id="Layer34" style="position:absolute; left:0; top:1934px; width:775px; height:11px; z-index:38"> 
  <h1 align="center"><font color="#006699">Uma viagem de fotografia digital</font></h1>
  <h2 align="center"><font color="#993300">A imagem dentro de Portugal</font></h2>

Open in new window

Pedro ChagasWebmasterAuthor Commented:
Hi, in snippet code you can see a solution that work almost well.
What left? a little big!
When I write the final file with the end code, the code that comes from $preservado write without empty line, but the code from $codigo_novo write with code and a empty line next, code, empty, etc. You can see in this image:
 the final result of index.htm
Have any solution for this little problem?
can make improvements in the script?

The best regards, JC
//index.htm is the file I want edit and that file contain some information that I want preserve. novo_codigo.php contain the new code that I will join to the preserve code

//get the code that I want preserve
$lines = explode("\n", file_get_contents('index.htm'));

//scape special characters
array_map("htmlspecialchars", $lines);

//$min give me the first line I want and $max give me the last line (that was the interval)
$min = -1;
$max = 10;
foreach ($lines as $key => $var) {
  if ($key > $min && $key < $max) {
    $preservado[] = $var;

//get the code that with the changes
$lines = explode("\n", file_get_contents('novo_codigo.php'));

//scape special characters
array_map("htmlspecialchars", $lines);

$min = -1;
$max = 7;
foreach ($lines as $key => $var) {
  if ($key > $min && $key < $max) {
    $codigo_novo[] = $var;
$array_completo = array_merge($preservado, $codigo_novo);
$string_completa = implode("\n", $array_completo);

$esvaziar = fopen("index.htm", "w");
fwrite($esvaziar, "");

$encher = fopen("index.htm", "w");
fwrite($encher, $string_completa);

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Empty lines are sometimes a result of using the wrong end-of-line characters.  Different systems use different signals for the end of a line.  In PHP, there is a predefined constant that is context-aware, giving the correct end-of-line characters for whatever OS you are using.  It is PHP_EOL and you use it like this:

echo "This is a line" . PHP_EOl;

I am not sure if that is your issue here, but it looks like it could be the issue.  Best of luck, ~Ray
Pedro ChagasWebmasterAuthor Commented:
Hi, how I use PHP_EOl when I unload the array?
Can you give me a example for a array?

Ray PaseurCommented:
Sorry for my typo.  It is not PHP_EOl -- it is PHP_EOL

Looking at this line of code...
$lines = explode("\n", file_get_contents('novo_codigo.php'));

The use of \n in the explode means that if the lines are terminated with \r\n you will still be keeping the \r.  If you use PHP_EOL instead of "\n" you might get better results.  Of you could use something like this:

$lines = file('novo_codigo.php');
Pedro ChagasWebmasterAuthor Commented:
With function "file" is worst, now I get empty lines too in the first array.
But no problem, you have reason, <<Empty lines are sometimes a result of using the wrong end-of-line characters>>, I write the file novo_codigo.php in Dreamweaver and work well now. I had previously edited the file in Cute FTP edit files.
So, my soluttion not will works for all files, but will work well for files write in Dreamweaver and identical software.

Thanks for help.

Pedro ChagasWebmasterAuthor Commented:
I will closing because I got the solution with myself.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.