Hi, i was wondering, if i include sth to a page, that it would automaticly find my defined number and replace it with an other number.
What script should i use? :/:
Thanks. :8:
Printable View
Hi, i was wondering, if i include sth to a page, that it would automaticly find my defined number and replace it with an other number.
What script should i use? :/:
Thanks. :8:
file_put_contents might not work, so you can use fopen(), fwrite()..PHP Code:$file_contents = file_get_contents('this_page.php');
$file_contents = str_replace('this','that',$file_contents);
file_put_contents($file_contents,'this_page.php');
Oh hell,
Files are easy. First you need to get a 'handle' on the file. Pretty much, fopen() is the same as taking a piece of paper out of the filing cabinet and holding it in your hand.PHP Code:$filename = 'this_page.php'; //the file name
$find = 'find this'; //Find this
$replace = 'replace with this'; //Replace with this
$file = fopen($filename,'r+'); //Open the file for read+write [r+]
$contents = fread($file, filesize($filename)); // Grab Contents
$contents = str_replace($find,$replace,$contents); // Replace Contents
fwrite($file,$contents); //Write updated content to file
fclose($file); //Close the file
Then we want to read the file. So fread() is like, Okay, we're holding a file, so let's read it for a little bit. We need to note how long we read the file. If you want to read the entire file, you can use filesize($filename) to get the length of the entire file.
So reading the file sends the data from the file to a variable. Once it's in a variable, we can replace parts of the file using str_replace().
str_replace is self-explanatory. It searches a string for a given 'find' value, and will replace it with the given 'replace' value, every time it's seen in the given string. So if you have a string like 'Hi grandpa!', and you use "str_replace('a','o',$string)", the string will now read, 'Hi grondpo!' - - - Simple.
Now fwrite() is like picking up a pencil and writing to that file you picked up with your hand. So you're handling a file, and now you want to take a pencil to it. So you just put the content in the second parameter. The first is obviously the handle, that's how the pencil knows what file to write to.. The one that's in your 'hand'...
Then when we're all done with the file, we set it back in the file cabinet with 'fclose();'
Remember, we need to use the handle on everything, even fclose(). You're putting the file from your hand, away to the filing cabinet with fclose($file).
Easy enough?