PHP Coders

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • donkevlar
    Confirmed User
    • Sep 2006
    • 4325

    #1

    PHP Coders

    Was wondering how to code something like this.. or if there was a script that does this. Php or otherwise.

    I want to take a bit of text with variables in it, duplicate it 50 times, and replace each variable with certain text that I have line by line in text files.

    Anyone?
    [email protected]
  • ungratefulninja
    Confirmed User
    • Apr 2006
    • 682

    #2
    It's 4am and I haven't gone to bed yet, so I'm not going to be super helpful... but you could just store the strings you're wanting to use in an array and then either randomly or in order substitute into your "bit of text."

    I do something kinda like that on some pages for an ad rotator. There is a php include that calls a separate file that I have different banners/ad-copy.. it just displays them randomly so surfers don't keep seeing the same one. I didn't want to bother creating a database for something so simple..

    Comment

    • k0nr4d
      Confirmed User
      • Aug 2006
      • 9231

      #3
      str_replace();
      Mechanical Bunny Media
      Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

      Comment

      • k0nr4d
        Confirmed User
        • Aug 2006
        • 9231

        #4
        err nm sorry im drunka nd didn't read entire post.

        First off you wanna do file_get_contents() to read the textfile in,
        Then explode(); on \n's
        then foreach($array as $i) { $var = str_replace('variable',$i,$originalstring); }
        Mechanical Bunny Media
        Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

        Comment

        • ungratefulninja
          Confirmed User
          • Apr 2006
          • 682

          #5
          That's pretty good for being drunk.

          Comment

          • k0nr4d
            Confirmed User
            • Aug 2006
            • 9231

            #6
            what can i say, im talented :P
            Mechanical Bunny Media
            Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

            Comment

            • nation-x
              Confirmed User
              • Mar 2004
              • 5370

              #7
              Originally posted by k0nr4d
              err nm sorry im drunka nd didn't read entire post.

              First off you wanna do file_get_contents() to read the textfile in,
              Then explode(); on \n's
              then foreach($array as $i) { $var = str_replace('variable',$i,$originalstring); }
              PHP has a built in function for that http://www.php.net/file that reads the file and puts each line of the file into an array element.

              if you have to files that are exactly the same length in lines then you just have to read them both in... iterate the arrays... make your replacements and output the result.

              Code:
              $file1 = file('path_to_first_file');
              $file2 = file('path_to_second_file'); //assuming that this file contains delimited text for the purpose of this example
              
              $line_iterator = 0;
              foreach ($file1 as $line) {
                  if (!empy($line)) {
                      list($var1, $var2, $var3) = explode(',', $file2[$line_iterator]); //pull variable from corresponding line of file #2 that are comma delimited
                      $temp_line = str_replace('<{var1}>', $var1, $line);
                      $temp_line = str_replace('<{var2}>', $var2, $temp_line);
                      $temp_line = str_replace('<{var3}>', $var3, $temp_line);
                      $output .= $temp_line; //add the line to the output
                      unset($temp_line);
                  }
                  $line_iterator++;
              }
              echo $output;
              Last edited by nation-x; 03-04-2007, 03:57 AM.

              Comment

              • nation-x
                Confirmed User
                • Mar 2004
                • 5370

                #8
                Code:
                if (!empy($line)) {
                This needs to be

                Code:
                if (!empty($line)) {

                Comment

                Working...