Replace and inserting strings within .txt files with the use of regex

Νίκος nikos.the.gr33k at gmail.com
Sun Aug 8 13:41:21 CEST 2010


On 8 Αύγ, 13:13, Thomas Jollans <tho... at jollans.com> wrote:
> On 08/08/2010 11:21 AM, Νίκος wrote:
>
> > Please help me adjust it, if need extra modification for more php tags
> > replacing.
>
> Have you tried it ? I haven't, but I see no immediate reason why it
> wouldn't work with multiple PHP blocks.
>
>
>
>
>
> > #!/usr/bin/python
>
> > import cgitb; cgitb.enable()
> > import cgi, re, os
>
> > print ( "Content-type: text/html; charset=UTF-8 \n" )
>
> > id = 0  # unique page_id
>
> > for currdir, files, dirs in os.walk('data'):
>
> >     for f in files:
>
> >         if f.endswith('php'):
>
> >             # get abs path to filename
> >             src_f = join(currdir,f)
>
> >             # open php src file
> >             f = open(src_f, 'r')
> >             src_data = f.read()         # read contents of PHP file
> >             f.close()
> >             print 'reading from %s' % src_f
>
> >             # replace tags
> >             src_data = src_data.replace('<%', '')
> >             src_data = src_data.replace('%>', '')
>
> Did you read the script before posting? ;-)
> Here, you remove ASP-style tags. Which is fine, PHP supports them if you
> configure it that way, but you probably didn't. Change this to the start
> and end tags you actually use, and, if you use multiple forms (such as
> <?php vs <?), then add another line or two.
>
>
>
> >             print 'replacing php tags'
>
> >             # add ID
> >             src_data = ( '<!-- %d -->' % id ) + src_data
> >             id += 1
> >             print 'adding unique page_id'
>
> >             # create new file with .html extension
> >             src_file = src_file.replace('.php', '.html')
>
> >             # open newly created html file for insertid data
> >             dest_f = open(src_f, 'w')
> >             dest_f.write(src_data)      # write contents
> >             dest_f.close()
> >             print 'writing to %s' % dest_f

Yes i have read the code very well and by mistake i wrote '<%>'
instead of '<?'

I was so dizzy and confused yesterday that i forgot to metnion that
not only i need removal of php openign and closing tags but whaevers
data lurks inside those tags as well ebcause now with the 'counter.py'
script i wrote the html fiels would open ftm there and substitute the
tempalte variabels like %(counter)d

Also before the

</body>
</html>

of every html file afetr removing the tags this line must be
inserted(this holds the template variable) that 'counter.py' uses to
produce data

<br><br><center><h4><font color=green> Αριθμός Επισκεπτών: %(counter)d
</h4>

After making this modifications then i can trst the script to a COPY
of the original data in my pc.

*In my pc i run Windows 7 while remote web hosting setup uses Linux
Servers.
*That wont be a problem right?



More information about the Python-list mailing list