static text generation

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
damithc
Forum Newbie
Posts: 22
Joined: Sat Apr 10, 2004 12:18 am
Location: singapore
Contact:

static text generation

Post by damithc »

I'm doing a bit of research on 'generative programming languages'. Could anybody please enlighten me on static text generation (as opposed to dynamic html generation) capabilities of PHP, if any?
For example, is there a straight forward way I can use PHP to generate a bunch of text files with slight variations (in a non-web context)?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sure: fwrite()
User avatar
damithc
Forum Newbie
Posts: 22
Joined: Sat Apr 10, 2004 12:18 am
Location: singapore
Contact:

Post by damithc »

Thanks for the super fast reply. :D

Possibly stupid question (i'm no PHP expert :oops:) : imagine i don't have a php enebled web server(Since i don't need any dynamic web pages). Can i still write some sort of PHP script and generate the said text files using that script (a PHP preprocessor kind o thing)?

I guess it's within the capability of the language. But is the necessary infrastructure available?

Does that make sense at all?
:?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

yeah. php can be run from a command line interface. You can have your scripts generate all the content you want.. it can even upload it all to the end server when it's done.. it's quite powerful.
User avatar
damithc
Forum Newbie
Posts: 22
Joined: Sat Apr 10, 2004 12:18 am
Location: singapore
Contact:

Post by damithc »

Gr8. Thanks a lot feyd for the replies. Will look in to PHP-CLI :)
User avatar
damithc
Forum Newbie
Posts: 22
Joined: Sat Apr 10, 2004 12:18 am
Location: singapore
Contact:

Post by damithc »

tried out the PHP-CLI. But still could not find a 'straight forward' way to use PHP in a generative sense. Let me give an example. If i want to generate 4 copies of following text for Name=a,b,c,d and save those in files a.txt b.txt c.txt and d.txt respectively, what would be the direct way of doing this in PHP (let's say using command line)?

-------------------
Dear Mr [Name]
blah blah blah...
-Thank you
-------------------
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Not sure if you'd class this as 'straight forward' but it's just one way of doing it.

Code: Select all

<?php

//array of names we want to generate the .txt files for
$names = array('Smith', 'Jones', 'Anderson');

//loop over them
foreach($names as $name){
  //turn on output buffering to generate the output/txt file
  ob_start();
  //require our txt 'template'
  require 'letter.php';
  $output = ob_get_contents();
  ob_end_clean();
  //open the txt file
  $fp = fopen($name.'.txt', 'w');
  //write the output to the file
  fputs($fp, $output);
  fclose($fp);
}

echo 'Finished';

?>
Where letter.php would look like:

Code: Select all

Dear Mr <?php echo $name ?>

blah blah blah...
-Thank you
User avatar
damithc
Forum Newbie
Posts: 22
Joined: Sat Apr 10, 2004 12:18 am
Location: singapore
Contact:

Post by damithc »

Thanx very much markl999. It works and it's the most 'straight forward' method I've seen so for (I've seen only one method so far :wink: )
Thanx again. Let's wait and see if there are any other suggestions.
User avatar
damithc
Forum Newbie
Posts: 22
Joined: Sat Apr 10, 2004 12:18 am
Location: singapore
Contact:

Post by damithc »

Thanx for all who helped me in this thread. With your help, i was able to move forward faster in my research. And I must confess I'm impressed by PHP's power :)

Now I'm looking into meta-programming aspect of PHP (if any).

For instance, can PHP used for something like below?

$(B($($A)))) should evaluate to 'ccc' if $A='B', $B='C' and $BC='ccc'. That is, recursively evaluated until a literal value is reached.

I know this is invalid syntax. But is there some other way to accomplish the same :?:
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Sure.

Code: Select all

<?php
$A='B';
$B='C';
$BC='ccc';
echo ${$A.$B};
?>
Have a read of variable variables for more info ;)
User avatar
damithc
Forum Newbie
Posts: 22
Joined: Sat Apr 10, 2004 12:18 am
Location: singapore
Contact:

Post by damithc »

Grand. thanks a lot markl999 :D
just out of curiosity, do you have anything you wish PHP could do, but it cannot? :twisted:
i guess not much eh? :wink:
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

damithc wrote: just out of curiosity, do you have anything you wish PHP could do, but it cannot? :twisted:
Only one thing on my list..

The abillity to perform an action at a regular interval. Having users required to install cronjobs (on unix) or scheduler entries (on windows) is difficult.

PHP is already a persistent module (mod_php), so it should in theory be able to do such a thing.
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

mod_php is a module, not an application. That's why it has to be run from another bit of software to do anything. In most cases, that's Apache. PHP isn't its own operating system or daemon, so it can't just run itself at predefined times. It has to be run by something.

Anyway, it's not as if it's hard to schedule a process on any system... cronjobs and scheduled tasks make it trivial. :)

I've only found a couple of things PHP can't do out of the box, but with the right extensions could very easily do.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

dave420 wrote:mod_php is a module, not an application. That's why it has to be run from another bit of software to do anything. In most cases, that's Apache. PHP isn't its own operating system or daemon, so it can't just run itself at predefined times. It has to be run by something.
You misunderstand my point.

Apache runs as a daemon, and mod_php runs in Apache, thus mod_php has the POSSIBILITY (if re-architected) to run code at scheduled intervals, or realtime (not dependent on a script).

Yes, mod_php has to be run by Apache - and it is. So when it is, it would be very nice to be able to give it scripts to run realtime.
dave420 wrote: Anyway, it's not as if it's hard to schedule a process on any system... cronjobs and scheduled tasks make it trivial. :)
No, it doesn't. As someone who distributes a fairly popular (top 20 on freshmeat at one point) web-based game, that is my number one compaint with PHP. Making a cross-platform scheduler process isnt just difficult, it requires EXTENSIVE admin interaction. Not to mention, not all shared hosts have shell access, making setting up cron even more complicated. Don't even get me started on how "easy" it is to setup a background, scheduled event thats browser-driven on Microsoft OS's.

It's not easy. If you think it is, I'd love to see code for a cross-platform, no-admin-interaction setup of a scheduled event, because in my experience, you can't do it.
dave420 wrote: I've only found a couple of things PHP can't do out of the box, but with the right extensions could very easily do.
I agree. Like I said, the ONLY major issue I've had is with scheduled/recurring events.
dave420
Forum Contributor
Posts: 106
Joined: Tue Feb 17, 2004 8:03 am

Post by dave420 »

PHP is a language, and the PHP software is an interpreter. The interpreter can't just gain consciousness and start running bits of code all on its own - it has to be called by something else. If you want PHP to run at a certain time, something has to run PHP at a certain time. PHP code can't just run itself. Neither can perl, java, C, C++ or anything else. Your gripe seems to be with a lack of cross-platform admin-less scheduling software, not a shortcoming with PHP.
Post Reply