GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   PHP Coders (https://gfy.com/showthread.php?t=711485)

donkevlar 03-04-2007 02:58 AM

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? :helpme

ungratefulninja 03-04-2007 03:24 AM

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..

k0nr4d 03-04-2007 03:37 AM

str_replace();

k0nr4d 03-04-2007 03:40 AM

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); }

ungratefulninja 03-04-2007 04:01 AM

That's pretty good for being drunk.

k0nr4d 03-04-2007 04:40 AM

what can i say, im talented :P

nation-x 03-04-2007 05:56 AM

Quote:

Originally Posted by k0nr4d (Post 12010878)
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;


nation-x 03-04-2007 06:02 AM

Code:

if (!empy($line)) {
This needs to be

Code:

if (!empty($line)) {


All times are GMT -7. The time now is 09:27 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123