php-general Digest 13 Jun 2010 10:52:26 -0000 Issue 6796

Topics (messages 306076 through 306083):

Re: How to change the first line of a text file
        306076 by: Ashley Sheridan
        306077 by: Steve
        306078 by: mcgiddin.gmail.com
        306079 by: mcgiddin.gmail.com

Re: is <?= good?
        306080 by: Paul M Foster

howto run sql script with php?
        306081 by: Stephan Ebelt
        306082 by: Peter Lind
        306083 by: Stephan Ebelt

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On Sat, 2010-06-12 at 14:10 -0700, Richard Kurth wrote:

> I have around 6000 text files and I need to change the first line on every 
> one of them.
> 
> The first line is the title of the article and I need it look like this 
> <title>the name of the article</title>
> 
> every file has the first line and the it starts the article on the second 
> line
> 
> The files are in many directory under one main directory
> like this
> 
> top directory
>             fashion-school
>             Dogs
>             DentalAssistant
>             etc..........
> 
> I need a script that will loop through each directory look at each file 
> extract the first line and replace it with the example above.
> I have figured out how to pull the first line into an array but I don't no 
> where to go from there to look in each directory and the write the data back 
> at the top.
> 
> $f = file('GENERAL HISTORY OF DOGS.txt');
> $a = '<title>' . $f[0] . '</title>';
> echo $a;
>  
> 
> 


Rather than use PHP for this I would look at using the find and sed
tools to edit the files.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--- End Message ---
--- Begin Message ---
On 6/12/2010 2:10 PM, Richard Kurth wrote:

I have around 6000 text files and I need to change the first line on every one of them.

The first line is the title of the article and I need it look like this <title>the name of the article</title>

every file has the first line and the it starts the article on the second line

The files are in many directory under one main directory
like this

top directory
           fashion-school
           Dogs
           DentalAssistant
           etc..........

I need a script that will loop through each directory look at each file extract the first line and replace it with the example above. I have figured out how to pull the first line into an array but I don't no where to go from there to look in each directory and the write the data back at the top.

$f = file('GENERAL HISTORY OF DOGS.txt');
$a = '<title>' . $f[0] . '</title>';
echo $a;




Something like this should do what you want: http://pastebin.com/jKvAiGYa

--- End Message ---
--- Begin Message ---
If you're running on a *nix system you may want to look at using find, xargs, 
and sed.  Sorry I can't give you the syntax at the moment but you can google 
for the three of them and find several good examples.

Matt

Sent via BlackBerry by AT&T

-----Original Message-----
From: Steve <ad...@ultramegatech.com>
Date: Sat, 12 Jun 2010 15:24:39 
To: <php-gene...@lists.php.net>
Subject: Re: [PHP] How to change the first line of a text file
On 6/12/2010 2:10 PM, Richard Kurth wrote:
>
> I have around 6000 text files and I need to change the first line on 
> every one of them.
>
> The first line is the title of the article and I need it look like 
> this <title>the name of the article</title>
>
> every file has the first line and the it starts the article on the 
> second line
>
> The files are in many directory under one main directory
> like this
>
> top directory
>            fashion-school
>            Dogs
>            DentalAssistant
>            etc..........
>
> I need a script that will loop through each directory look at each 
> file extract the first line and replace it with the example above.
> I have figured out how to pull the first line into an array but I 
> don't no where to go from there to look in each directory and the 
> write the data back at the top.
>
> $f = file('GENERAL HISTORY OF DOGS.txt');
> $a = '<title>' . $f[0] . '</title>';
> echo $a;
>
>
>

Something like this should do what you want: http://pastebin.com/jKvAiGYa

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--- End Message ---
--- Begin Message ---
The command would be something like this:

find /wwwdir -name \*.txt -print | xargs sed -i s/oldtext/newtext/gi

Matt


Sent via BlackBerry by AT&T

-----Original Message-----
From: Steve <ad...@ultramegatech.com>
Date: Sat, 12 Jun 2010 15:24:39 
To: <php-gene...@lists.php.net>
Subject: Re: [PHP] How to change the first line of a text file
On 6/12/2010 2:10 PM, Richard Kurth wrote:
>
> I have around 6000 text files and I need to change the first line on 
> every one of them.
>
> The first line is the title of the article and I need it look like 
> this <title>the name of the article</title>
>
> every file has the first line and the it starts the article on the 
> second line
>
> The files are in many directory under one main directory
> like this
>
> top directory
>            fashion-school
>            Dogs
>            DentalAssistant
>            etc..........
>
> I need a script that will loop through each directory look at each 
> file extract the first line and replace it with the example above.
> I have figured out how to pull the first line into an array but I 
> don't no where to go from there to look in each directory and the 
> write the data back at the top.
>
> $f = file('GENERAL HISTORY OF DOGS.txt');
> $a = '<title>' . $f[0] . '</title>';
> echo $a;
>
>
>

Something like this should do what you want: http://pastebin.com/jKvAiGYa

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


--- End Message ---
--- Begin Message ---
On Fri, Jun 11, 2010 at 05:29:50PM -0400, tedd wrote:

<snip>

> As for the Python "print" problem, I don't do Python -- so, it can
> win the lottery, or die, I don't care.

The issue, as I recall, is that for Python 3, they are changing print
(without parentheses) to require parentheses. This change alone (and
there are others like this in Python 3) will necessitate a *lot* of code
rewrites. Sometimes I wonder about Guido van Rossum.

>
> As for "don't embed a variable into a literal string and use
> preprocessing", as I said above, I often do this:
>
> <?php echo("The answer is $answer");?>
>
> and find no problems whatsoever in doing so.
>
> However, I wouldn't do this:
>
> <?php echo 'The answer is '.$answer; ?>
>
> OR, I would place a space before/after the . (i.e., 'The answer is '
> . $answer). However, I don't like doing that because I also program
> in other languages, which use the dot operator differently.

I tend to do it the way Tedd does, but I'm rethinking this. I recently
had occasion to do a lot of programming in C and Python, and one thing
stood out at me: I can't recall another language which allows
interpolation of variables within strings (without significant diddling,
if at all). It caused me great difficulty because I'm so used to doing
this in PHP. I can convert from '.' to '+' (as in most languages) in my
mind, but embedding variables in strings was a harder habit to break. My
opinion is that the dot operator used this way was a mistake for
PHP. Not using the dot operator to mean "concatenate" would mean we
could use it to replace the accursed '->' for class/method selection.
And the plus operator is obviously a more natural fit for string
concatenation.

And yes, if you're going to use the dot operator, surround it with
spaces for readability.

Paul

-- 
Paul M. Foster

--- End Message ---
--- Begin Message ---
Hello,

I am trying to run a .sql script produced with mysql-dump from a php script but
failed to find a rudimentary robust solution yet.

mysql_query() can only run one query at a time. Thus its necessary to somehow
parse the input file and separate the queries... which is difficult since the
syntax can be quite complex.

So far I managed to run most CREATE TABLE and INSERT queries just by
dropping newlines (\n) and explode()ing the input by ';'. Now I am stuck with a
CREATE TRIGGER statement because mysql-dump does this:

===
DELIMITER ;;
CREATE TRIGGER ... ON ...
FOR EACH ROW BEGIN ...
        ...
END;
;;
DELIMITER ;
===

exploding that string by ';' results in 4 (or more) queries which are invalid
syntax then. And I have no glue on what else mysql-dump may come up with on
other occasions.

does anyone know how to do that properly?

I dont favor importing the dumps via shell command like 'cat some.dump | mysql'
either. 

stephan


--- End Message ---
--- Begin Message ---
On 13 June 2010 11:05, Stephan Ebelt <s...@shared-files.de> wrote:
> Hello,
>
> I am trying to run a .sql script produced with mysql-dump from a php script 
> but
> failed to find a rudimentary robust solution yet.
>
> mysql_query() can only run one query at a time. Thus its necessary to somehow
> parse the input file and separate the queries... which is difficult since the
> syntax can be quite complex.
>

Use mysqli - it supports running multiple queries at once.

Regards
Peter


-- 
<hype>
WWW: http://plphp.dk / http://plind.dk
LinkedIn: http://www.linkedin.com/in/plind
BeWelcome/Couchsurfing: Fake51
Twitter: http://twitter.com/kafe15
</hype>

--- End Message ---
--- Begin Message ---
On Sun, Jun 13, 2010 at 11:29:14AM +0200, Peter Lind wrote:
> On 13 June 2010 11:05, Stephan Ebelt <s...@shared-files.de> wrote:
> > Hello,
> >
> > I am trying to run a .sql script produced with mysql-dump from a php script 
> > but
> > failed to find a rudimentary robust solution yet.
> >
> > mysql_query() can only run one query at a time. Thus its necessary to 
> > somehow
> > parse the input file and separate the queries... which is difficult since 
> > the
> > syntax can be quite complex.
> >
> 
> Use mysqli - it supports running multiple queries at once.

sometimes its as easy as that. I'll give it a try.

thanks,
stephan


--- End Message ---

Reply via email to