This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php - efficent way to get and remove first line in file
I need to remove first line form txt file using php. Could you show me example code how to do it? I know that it's easy but I don't know php:) Thanks!
You could do this:
$file = file_get_contents(SOME_FILE);
$arr = explode('\n\r', $file);
if (isset($arr[0])) unset ($arr[0]);
$string = implode('\n\r', $arr);
file_put_contents(SOME_FILE, $string);
You could have used the search function at least. You would have found this:
$contents = file($file, FILE_IGNORE_NEW_LINES);
$first_line = array_shift($contents);
file_put_contents($file, implode("\r\n", $contents));
Related
This question already has answers here:
Replace string in text file using PHP
(4 answers)
Closed 1 year ago.
i need to find a line and change it to whatever user will type in input i didnt write this code pls help
<?php
$myfile = 'test.txt';
$lines = file($myFile, FILE_IGNORE_NEW_LINES);
$lines[4] = $_POST['title'];
file_put_contents($myFile , implode("\n", $lines));
?>
i tryed lot of ways but non of them works and i only have this code . i am working on project idea is simple user will give type url of website i need to find a line and change it to <form action="process.php" method="post"> i need help its will be on same line so i dont need to change line
i searched lot of ways but does not works for me.
i am starter at php so...
There is an error in your code.
$lines = file($myFile, FILE_IGNORE_NEW_LINES);
should be
$lines = file($myfile, FILE_IGNORE_NEW_LINES);
because you're declaring
$myfile = 'test.txt';
Note the usage of smaller case f in file name.
Now, coming to your question.
You want to replace some lines in the file with user input.
Let's assume that you want to replace the text THIS SHOULD BE REPLACED.
For that, you could use the code something line below:
<?PHP
$myfile = './test.txt';
$lines = file($myfile, FILE_IGNORE_NEW_LINES);
$textToBeReplaced = "THIS SHOULD BE REPLACED";
$index = array_search($textToBeReplaced, $lines);
if (false !== $index) {
$lines[$index] = $_POST['title'] ?? '';
}
file_put_contents($myFile , implode("\n", $lines));
?>
This question already has answers here:
How to remove the querystring and get only the URL?
(16 answers)
Closed 2 years ago.
I would like to remove anything that follows after a specific set of characters (i.e. filetypes / extensions). I have tried numerous scripts I found online, but none really manage to do what I need, they either remove the file extension as well, or keep parts of the arguments that follow.
$urls = array(
'http://www.example.com/images/image1.jpg',
'http://www.example.com/images/image2.png?arg=value',
'http://www.example.com/images/image3.jpg?foo=bar',
'http://www.example.com/images/image4.gif?v=1',
'http://www.example.com/images/image5.bmp?x=y',
'http://www.example.com/images/image6.tiff?werdfs=234234'
);
Desired outcome:
http://www.example.com/images/image1.jpg
http://www.example.com/images/image2.png
http://www.example.com/images/image3.jpg
http://www.example.com/images/image4.gif
http://www.example.com/images/image5.bmp
http://www.example.com/images/image6.tiff
Maybe this one help you.
$re = '/^.*(?:\.)[a-zA-Z]+/m';
$urls = array(
'http://www.example.com/images/image1.jpg',
'http://www.example.com/images/image2.png?arg=value',
'http://www.example.com/images/image3.jpg?foo=bar',
'http://www.example.com/images/image4.gif?v=1',
'http://www.example.com/images/image5.bmp?x=y',
'http://www.example.com/images/image6.tiff?werdfs=234234',
'asdasd'
);
foreach ($urls as $url) {
preg_match($re, $url, $matches);
if ($matches) {
echo $matches[0];
echo "\n";
}
}
Output
http://www.example.com/images/image1.jpg
http://www.example.com/images/image2.png
http://www.example.com/images/image3.jpg
http://www.example.com/images/image4.gif
http://www.example.com/images/image5.bmp
http://www.example.com/images/image6.tiff
How about PHP's parse_url() and basename?
$inName = $urls[0]; // for example
$newName = parse_url($inName,PHP_URL_SCHEME)
. parse_url($inName,PHP_URL_HOST)
. parse_url($inName,PHP_URL_PATH)
. basename($inName);
This question already has answers here:
PHP, get file name without file extension
(18 answers)
Closed 4 years ago.
I'm looking for a way to get a substring in php. Given string is:
"abc/def/ghi/name.extension"
I only want to have 'name', and the number of forward slashes is not known and not the same number either. Tried it with substring, but I get stuck in the elimination of the /
Anyone an idea how to solve this?
Help is greatly appreciated!
You can use pathinfo:
$str = "abc/def/ghi/name.extension";
$name = \pathinfo( $str, \PATHINFO_FILENAME );
Try this. First split the string into an array with explode () and then split the last element by the dot.
$a = 'abc/def/ghi/name.extension';
$b = explode ('/', $a);
$b = $b[count ($b) - 1];
$c = explode ('.', $b)[0];
You can try this for filename.
$filename = pathinfo("abc/def/ghi/name.extension", PATHINFO_FILENAME);
echo $filename;
and this for file extension.
$ext = pathinfo("abc/def/ghi/name.extension", PATHINFO_EXTENSION);
echo $ext;
This question already has answers here:
Replace a whole line where a particular word is found in a text file
(12 answers)
Closed last year.
Need to find a simple solution to the following:
I have a php file that, when executed, should be able to replace a specific line of code within itself, with a different value on that line.
I came up with this so far:
$file = "file.php";
$content = file($file);
foreach($content as $lineNumber => &$lineContent) {
if($lineNumber == 19) {
$lineContent .= "replacement_string";
}
}
$allContent = implode("", $content);
file_put_contents($file, $allContent);
However, that does not replace the specific line. It adds the new string on a new line and that's it. I need that specific line ERASED and then REPLACED with the new string, on that line.
How would I continue about doing that? I'd love some pointers.
Since file() creates an array, make use of the index to select the line.
Don't forget that array indexes start at 0!
$file = "file.php";
$content = file($file);
$content[19] = "replacement_string\r\n";
$allContent = implode("", $content);
file_put_contents($file, $allContent);
Your problem is the .= in the $lineContent .= "replacement_string"; line. Just use = or use the str_replace() or str_ireplace() function.
This question already has answers here:
Parsing domain from a URL
(19 answers)
Closed 8 years ago.
http://www.example.com/hu/link
Using PHP how can I only keep the hu? Please note that the link is only one variable, it may be anything
You can use explode
$exploded = explode('/', 'http://www.example.com/hu/link');
$value = $exploded[3];
One solution is this:
$split = explode("/", $_SERVER["REQUEST_URI"]);
echo $split[1];
$url = "http://www.example.com/hu/link";
$split_url = explode('/', $url);
echo $split_url[3];
Output:
hu
You can use a regular expression preg_match like this:
$url = 'http://www.example.com/hu/link';
preg_match('/.*www.*\/(.*)\//i',$url, $matches);
print_r($matches[1]);