How to replace url with php
why this code not work .
please help
$mylogo = 'http://example.com/images/logo.png';
$inputmylogo = 'http://example.com/image.png';
$file = 'setting.php';
$content = file_get_contents($file);
var_dump(preg_replace('/'.$mylogo.'/', $inputmylogo, $content));
You want to employ str_replace here:
$mylogo = 'http://example.com/images/logo.png';
$inputmylogo = 'http://example.com/image.png';
$file = 'setting.php';
$content = file_get_contents($file);
var_dump(str_replace('/'.$mylogo.'/', $inputmylogo, $content));
Related
I want to ovewrite some labeled data in PHP Config file which looks like this:
define("__dbhost__", '{DB_HOST}');
define("__dbname__", '{DB_NAME}');
define("__dbuser__", '{DB_USER}');
define("__dbpass__", '{DB_PASS}');
define("__dbport__", '{DB_PORT}');
So the file is really simple.
The way i'm trying to replace data is by:
$searchF = array('{DB_HOST}','{DB_NAME}','{DB_USER}','{DB_PASS}', '{DB_PORT}');
$replaceW = array('a','c','d','b','a');
$fname = "../app/config/database.php";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));
$content = str_replace($searchF, $replaceW, $content);
$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);
But once finished and after I open the file I get
Resource id #16
Inside of it.
What's going on and why typical str_replace won't work for this case?
Just read the entire file into a string:
$searchF = array('{DB_HOST}','{DB_NAME}','{DB_USER}','{DB_PASS}', '{DB_PORT}');
$replaceW = array('a','c','d','b','a');
$fname = "../app/config/database.php";
$content = file_get_contents($fname);
$content = str_replace($searchF, $replaceW, $content);
file_put_contents($fname, $content);
Read file into string
Replace on that string
Write string to file
So i found this code which lets be write to a specific line
function SetSiteName(){
global $session, $database, $form;
$filepathname = "include/classes/constants.php";
$target = 'sitename';
$newline = 'define("sitename", "Testing.");';
$stats = file($filepathname, FILE_IGNORE_NEW_LINES);
$offset = array_search($target,$stats) +32;
array_splice($stats, $offset, 0, $newline);
file_put_contents($filepathname, join("\n", $stats));
header("Location: ".$session->referrer);
}
however it will not overwrite whats on that line it'll go to the next line and put the data in.. I'd like to make it overwrite what currently is on that line?
Any thoughts?
You can overwrite a line of a file with this code.
$filename = "file.txt";
$content = file_get_contents($filename);
$lines_array = explode(PHP_EOL, $content);
//overwrite the line that you want.
$lines_array[5] = "New text at line 6!";
file_put_contents($filename, implode(PHP_EOL, $lines_array));
I save to file data from form:
$name = $_POST['name'];
$url = $_POST['url'];
$comm = $_POST['comm'];
$data["name"]=$name;
$data["url"]=$url;
$data["comm"]=$comm;
file_put_contents("db.txt", serialize($data));
Now, I would like to read this file record by record.
$file_handle = fopen("db.txt", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
$arr = unserialize($line);
var_dump($arr);
}
fclose($file_handle);
But this code read only last record. How to read all file?
Replace file_put_contents("db.txt", serialize($data)); to
file_put_contents("db.txt", PHP_EOL .serialize($data), FILE_APPEND);
file_put_contents("db.txt", serialize($data));// will over write the file again and again. so you cant able to read all the data. FILE_APPEND helps to append the data And PHP_EOL helps to leave a line breake.
Hi i try this code for your solution:
<?php
$name = "rdn";
$url = "http://google.it";
$comm = "com";
$data["name"]=$name;
$data["url"]=$url;
$data["comm"]=$comm;
file_put_contents("db.txt", serialize($data)."\n",FILE_APPEND);
$fh = fopen('db.txt','r');
while ($line = fgets($fh)) {
// <... Do your work with the line ...>
var_dump(unserialize($line));
}
fclose($fh);
?>
without "\n" don't work!
sorry because I don't know how to ask the question
I have here a code in php
$productDetails['country'][0]['image']
which output is:
http://localhost/wine-works.co.uk-tester/wp-content/uploads/2013/09/canada-wine_shop.jpg
how do I change the filename to look like this:
http://localhost/wine-works.co.uk-tester/wp-content/uploads/2013/09/canada-wine_shop-25x17.jpg
$d = pathinfo($productDetails['country'][0]['image']);
echo $new_name = $d['dirname'].'/'.$d['filename'].'-25x17.'.$d['extension'];
$str= 'http://localhost/wine-works.co.uk-tester/wp-content/uploads/2013/09/canada-wine_shop.jpg';
$suffix = '-25x17';
$name = substr($str,strrpos($str,'/')+1);
$leftString = substr($str,0,strrpos($str,'/')+1);
$extension = substr($name,strrpos($name,'.'));
$nameNoExtension = substr($name,0,strrpos($name,'.'));
$string = $nameNoExtension.$suffix.$extension;
echo $leftString.$string;
//http://localhost/wine-works.co.uk-tester/wp-content/uploads/2013/09/canada-wine_shop-25x17.jpg
I'm writing some code and I need to write a number to a specific line. Here's what I have so far:
<?php
$statsloc = getcwd() . "/stats/stats.txt";
$handle = fopen($statsloc, 'r+');
for($linei = 0; $linei < $zone; $linei++) $line = fgets($handle);
$line = trim($line);
echo $line;
$line++;
echo $line;
I don't know where to continue after this. I need to write $line to that line, while maintaining all the other lines.
you can use file to get the file as an array of lines, then change the line you need, and rewrite the whole lot back to the file.
<?php
$filename = getcwd() . "/stats/stats.txt";
$line_i_am_looking_for = 123;
$lines = file( $filename , FILE_IGNORE_NEW_LINES );
$lines[$line_i_am_looking_for] = 'my modified line';
file_put_contents( $filename , implode( "\n", $lines ) );
This should work. It will get rather inefficient if the file is too large though, so it depends on your situation if this is a good answer or not.
$stats = file('/path/to/stats', FILE_IGNORE_NEW_LINES); // read file into array
$line = $stats[$offset]; // read line
array_splice($stats, $offset, 0, $newline); // insert $newline at $offset
file_put_contents('/path/to/stats', join("\n", $stats)); // write to file
I encountered this today and wanted to solve using the 2 answers posted but that didn't work. I had to change it to this:
<?php
$filepathname = "./stats.txt";
$target = "1234";
$newline = "after 1234";
$stats = file($filepathname, FILE_IGNORE_NEW_LINES);
$offset = array_search($target,$stats) +1;
array_splice($stats, $offset, 0, $newline);
file_put_contents($filepathname, join("\n", $stats));
?>
Because these lines don't work since the arg of the array is not an index:
$line = $stats[$offset];
$lines[$line_i_am_looking_for] = 'my modified line';
Had to add that +1 to have the new line under the searched text.