SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast Cartman's Avatar
    Join Date
    Oct 2004
    Location
    Mount Laurel, NJ
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating files based off of a template in PHP?

    I've got a list of about 200 filenames that I need created in a specific directory. All will work off of a common template, with just one parameter changed inside of each file. (I need the filename passed into the newly created file)

    If this is unclear, maybe this will help.

    Index.html calls theatre/1612.html. 1612.html doesn't exist. template.html exists, and 1612.html should be created as an exact copy of template.html, but inside of 1612.html there's a reference to "template", and that needs to be changed to "1612".

    Can this be done easily? Any help would be greatly appreciated!

  2. #2
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can use substr_replace, or preg_replace (if that string repeats many times and you wana change one specific occurence
    get the file with file_get_contents, then change that field and write the file

    and you can put that field in template.html like
    {{SPECIAL}}
    and then
    PHP Code:
    $content file_get_contents('template.html');
    $content str_replace('{{SPECIAL}}','1612',$content);

    $fh fopen('1612.html','w');
    fwrite($fh,$content);
    flcose($fh); 
    some thing like that, in a while loop or dynmaically with php

    hope this helps
    ---------------------------
    Errors = Improved Programming.
    My Site

  3. #3
    SitePoint Enthusiast Cartman's Avatar
    Join Date
    Oct 2004
    Location
    Mount Laurel, NJ
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That sounds great, I'm just not having luck building the loop.. The text file that has the 200+ filenames, how do I have it read one line at a time, and then parse the filename to pass it to replace {{SPECIAL}}?

    I can post a sample if you need it.. Thanks man!

  4. #4
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, please post the sample
    and if its only file name per line then you can use
    PHP Code:
    $lineArr file('filelist.txt')
    echo 
    '<pre>';
    print_r($lineArr);
    echo 
    '</pre>'
    it assumes you have your file like

    file1.html
    file2.html

    ....
    filen.html

    and if so, then you can go through that array in foreach loop like
    PHP Code:
    forach($lineArr as $key=>$value)
    {
    $fileNameArr explode('.',$value);//$fileNameArr[0] = filename [1]=extension like .html or so, and if you have files like  file_name.inc.php, then it'll take the first part

     
    $content file_get_contents('template.html');
    $content str_replace('{{SPECIAL}}',$fileNameArr[0],$content);

    $fh fopen($value,'w');
    fwrite($fh,$content);
    flcose($fh); 

    hope this helps
    ---------------------------
    Errors = Improved Programming.
    My Site

  5. #5
    SitePoint Enthusiast Cartman's Avatar
    Join Date
    Oct 2004
    Location
    Mount Laurel, NJ
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That looks perfect, I'll give that a shot and see what happens. Thanks again!

  6. #6
    SitePoint Enthusiast Cartman's Avatar
    Join Date
    Oct 2004
    Location
    Mount Laurel, NJ
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just wanted to say that jaswinder_rana went well beyond the call of duty, and REALLY helped me out! Thanks buddy!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •