I have a txt file in the server and it contains lines sth like that
one
two
three
four
five
i want to make a function that checks if a word exists in these lines.
any help is appreciated.
thank you
Here is how you may proceed with it:
$contents = file_get_contents('yourfile.txt');
$search_keyword = 'four';
// check if word is there
if (strpos($contents, $search_keyword) !== FALSE){
echo "$search_keyword was found !!";
}
else{
echo "$search_keyword was NOT found !!";
}
Does this really need to be done in php? You've just described the UNIX grep utility.
You could do it like:
$data = file($path_to_file,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (in_array($data,'word')) {
// do something
}
That is a simple hack but it should work.
Related
i have a problem i couldn't figure out since im self-taught and still exploring the php world
so i have a text file that looks like this:
951753159
456787541
123156488
748651651
and i got an url with a variable
http://example.com/mypage.php?variable=951753159
what i want is to check if the url variable matches one of the txt file lines in order to execute a code
i already tried this
$search = $_GET["variable"];
$file = "variables.txt";
if (preg_match('/^' . $search . '$/m', file_get_contents($file))) {
THE CODE THAT I WANT TO EXECUTE
}
but for some reason it matches the whole content of the file
any help is highly appreciated
Thanks in advance :)
Try with an array from file():
$lines = file("variables.txt", FILE_IGNORE_NEW_LINES);
if(in_array($_GET["variable"], $lines)) {
// YES FOUND
} else {
// NOT FOUND
}
From the documentation on `file_get_contents', the entire contents of the file are read as a string. So that is why it is matching against the entire file.
The command that you want to use is file, this reads the file into an array of each line.
I would
Use file to read the file into an array.
Then array_flip the array so that it's values are now the keys
Which allows me to isset($array[$key])
You can do this.
<?php
#$search = $_GET["variable"];
$search = '123156488';
$file_txt = "content.txt";
$file = file($file_txt);//convert the txt in array
foreach ($file as $key => $value) {
if (trim($search) == trim($value)) {
print "DO something! " . $value;
}
}?>
Regards.
Nelson.
First time I ask a question here and sorry for my english. So, here's my code:
function data1_show() {
$f = fopen("data/data1.txt", "r");
while (feof($f)!=true) {
$line = fgets($f);
print $line."<br>";
}
fclose($f);
print "<div id=\"s1\" class=\"s\"> </div>";
}
Not the best, but it works at least. But I want to read the text file inside the print "div /div", and this is the problem. I've tried several reading methods and none of them worked. I just can't figure it out.
You can do explode:
$array = explode('div', $text_data);
and then use $array and proper element of it that you want.
You can also use RegExp:
preg_match("/^div([a-zA-Z0-9_-\.\+\-]).*//div$/, $content, $res);
The regexp is better I haven't tested that.
in php we can check if file exist using
if(file_exists("destination/"))
{
condition
}
but what I wanted to do is...
for example I already have this file on my destination
hello_this_is_filename(2).doc
how would I know if there is a file in that directory having a name containing a character
hello_this_is_filename
I wanted to search that way because... if there is exists on that directory, what will I do is... renaming the file into
hello_this_is_filename(3).doc
I also need to count the existence of my search so I know what number I'm going to put like
(3), (4), (5) and so on
any help?
Use glob.
if (count(glob("destination/hello_this_is_filename*.doc"))) {
//...
}
Leveraging Marc B's suggestion and xdazz, I would do something as follows:
<?php
$files = glob("destination/hello_this_is_filename*");
if (count($files)) {
sort($files);
// last one contains the name we need to get the number of
preg_match("([\d+])", end($files), $matches);
$value = 0;
if (count($matches)) {
// increment by one
$value = $matches[0];
}
$newfilename = "destination/hello_this_is_filename (" . ++$value . ").doc";
?>
Sorry this is untested, but thought it provides others with the regexp work to actually do the incrementing...
I want to add four line of code in a PHP file if it found for string 'test' in the file anywhere. I google a lot on this, but didn't get exact solution :(
Please help me on this. Thanks in advance.
You can search your text in file like this:
$data = file_get_contents('file.ext');
if(strpos($data, 'text') !== FALSE)
{
$file_data = "Stuff you want to add\n";
$file_data .= $data;
file_put_contents('file.ext', $file_data);
}
For appending lines at the end of the file use
file_put_contents('file.ext', $text_to_append, FILE_APPEND | LOCK_EX);
FILE_APPEND flag is for appending at the end of file and LOCK_EX flag to prevent anyone else writing to the file at the same time
I am trying to create a text file with PHP in fopen("test.txt","a"). I have also tried with fopen("test.txt","w+").
The text file is created but I want to check for some string in test.text, that it exist or in test.txt it will not create duplicate entry. Can someone help me out?
Use this code:
$content = file_get_contents('test.txt');
if (str_pos('YOUR KEYWORD', $content) !== false){
// your keyword exists in the file
}
if (!file_exists("test.txt"))
{
///file does not exist
}
else
{
$f = fopen("test.txt", "a");
fwrite($f, "appending text");
}
searching in file:
$arr = file_get_contents("test.txt");
if (preg_match("/your_rexexp/i", $arr))
{
echo "text found";
}
If I understand you correctly, you want to insert new values into a textfile, but first check if the value already exists in it.
You can use fread() to get the contents of the file, and then through preg_match() you can check for values in the file.
If you are using PHP5 then you can use some of the new easy functions.
file_exists(filename) returns boolean
sayin if the file exists.
file_put_contents(filename,data) returns boolean saying if the write was successful.
file_get_contents(filename) returns the contents of the file (string).