Validate Form Against a Text File - php

I have a simple form input box on my site that the user enters a 4 digit code into & submits. I need that code to match a list of codes in a text file. If not, it just returns an error. If so, it would execute another function. Using PHP.
I'm struggling on how to compare against that text file, seems simple to me...
I appreciate all of your help, learned SOOO much from SO.
Ian

Since you haven't provided a sample of your file or PHP code, am submitting the following:
Considering data.txt contains and with no commas from a .csv file, this will work.
1111
1112
1113
PHP
<?php
$search = "1111";
$file = "data.txt";
if (preg_match('/^' . $search . '$/m', file_get_contents($file))) {
echo "$file DOES contains $search\n";
} else {
echo "$file does NOT contain $search\n";
}
This is another method which will work with or without a comma-seperated file:
1111,
1112,
1113,
PHP
<?php
$search = "1111";
$file = fopen("data.txt", "r") or die("Cannot open file!\n");
while ($line = fgets($file, 1024)) {
//if (preg_match("/\b1111\b/i", $line)) {
if (preg_match("/\b$search\b/i", $line)) {
echo "<b>Found match: " . $line . "</b>";
} else {
echo "No match: " . $line;
}
}
fclose($file);

Related

Regex PHP - Get specific text including new lines and multiple spaces

I was trying to get the text starting from css:{(some text...)} up to the ending bracket only, not including the texts below in another text file using php.
test.sample
just a sample text
css:{
"css/test.css",
"css/test2.css"
}
sample:text{
}
I'm using vscode/sublime search and replace tool to test my regex syntax and nothing is wrong, I successfully get the text that I want including all the new lines and spaces inside, but when i tried to apply it on php, the regex that I created doesn't work, it cannot find the text that im looking for.
here is my code:
myphp.php
$file = file_get_contents("src/page/test.sample");
echo $file . "<br>";
if (preg_match_all("/(css\s*\n*:\s*\n*\{\s*\n*)+((.|\n\S)*|(.|\n\s)*)(\n*)(\}\W)$/", $file)) {
echo "Success";
} else {
echo "Failed!";
}
This is my regex that I just created.
(css\s*\n*:\s*\n*{\s*\n*)+((.|\n\S)|(.|\n\s))(\n*)(}\W)$
Please help me, Im open for any suggestion, Im a newbie on regular expression, Im lacking on knowledge about the logic of it.
thanks.
Try this my friend:
<?php
$file = "testfile.php"; // call the file
$f = fopen($file, 'rb'); // open the file
$found = false;
while ($line = fgets($f, 1000)) { // read every line of the file
if ($found) {
echo $line;
continue;
}
if (strpos($line, "css:") !== FALSE) { // if we found the word 'css:' we print everything after that
$found = true;
}
}
Hey guys I found the solution! base on the answer of #alex
Dont really know if I am implementing this right.
Here is my code
$src = "src/page/darwin.al"; //get the source file
$file = fopen($src,"rb"); //I dont really know what 'rb' means, I guess it simply means, 'not a commong text file'?, search for it!
$found = false;
$css = false;
while($line = fgets($file)){ //read every line of text and assign that line of text in a variable $line
if(strpos(preg_replace("/\s*/", "", $line), "css:{") === 0){ //if the current line is == 'css:{' <the thing that Im looking for,
$found = true;
$css = true;
}elseif($css && strpos(preg_replace("/\s*/", "", $line),"}") === 0){ //If we are still inside the css block and found the '{'
echo $line;
break;
}
if ($found) {
echo preg_replace("/\s*/", "", $line); //remove every whitespace!
}
}
fclose($file);//close the file

Fwrite PHP - How to output full loop to file?

I have created a script that's working great, but I want to take it to the next level and create a file for this information automatically. The problem is that when I add my looped elements to using fwrite, it only writes the first entry to the file.
Sample code:
$totallines=count($lines);
$i=0;
foreach($lines as $line) {
$i=$i;
$trimmed = trim($line);
$linesgetdomain = explode('/',$trimmed);
$domain = $linesgetdomain[2];
$qmark = "?";
$front301qmark = "Front Part ";
$end301qmark = " End Part";
$lastpartqmark = "Last Part $domain";
if (strpos($trimmed,$qmark) !== false) {
strstr($trimmed, '?');
echo $front301qmark;
echo substr(strstr($trimmed, '?'), strlen('?'));
echo $end301qmark;
echo "<br>";
echo $lastpartqmark;
echo "<br>";
} else {
What I tried (placing this before } else {):
$fpqmark = fopen('test.txt', 'w');
fwrite($fpqmark, $front301qmark);
fwrite($fpqmark, substr(strstr($trimmed, '?'), strlen('?')));
fwrite($fpqmark, $end301qmark);
fwrite($fpqmark, PHP_EOL);
fwrite($fpqmark, $lastpartqmark);
fwrite($fpqmark, PHP_EOL);
fclose($fpqmark);
How can I write the entire loop to a file instead of just the first line? The output displays properly in normal format, but not when writing to a file.
Apologizes beforehand if this is a basic question. I'm new to PHP and my understandings of certain concepts are a bit vague. Sort of surprised I even managed to get this script to work. (This is only a small part of it, but if I can learn how to do this, I should be able to do the rest).
instead of using write,
$fpqmark = fopen('test.txt', 'w');
Use apend,
$fpqmark = fopen('test.txt', 'a');
The it will resolve your problem.

PHP - can't compare two "Strings" (?)

So this is my code:
$handle = #fopen("csgo_english.txt", "r"); //read line one by one
$paintkitsStarted = false;
while (!feof($handle)) // Start looping until there is no line anymore.
{
$buffer = fgets($handle, 4096); // Read a line.
$convertedBuffer = $buffer;
echo $convertedBuffer;
if($convertedBuffer == "// Paint Kits "){
$paintkitsStarted = true;
echo "Paintkit Line found! <br>";
}
if($convertedBuffer == "// END CRATE_COMMUNITY_10 "){
$paintkitsStarted = false;
echo "Paintkits ending here! <br>";
}
if($paintkitsStarted == true){
echo $buffer . "<br>";
}
I'm trying to read the .txt-File (which works great) and then comparing a line of it with a string. I already tried casting it to a string using:
$convertedBuffer = "$buffer";
and
$convertedBuffer = (string)$buffer;
as well as
$convertedBuffer = $buffer . "";
I don't know why this isn't working as it should, because I actually took the string I needed by copying it from the output using echo $buffer which should work.
If anyone has Ideas, I would be grateful to hear them :)
Ok, so I still don't know what the problem was. But something probably was wrong with the file, because I just copied the content of the original into a second one and after doing that and trying it with the second one, it worked. Don't ask me why ...

php reading from file and manipulating the data

I am having some difficulty with reading info from a text file. Is it possible to use php and get one line at a time, and compare that line to a variable, one character at a time? Every time I add the character searching algorithm it messes up. or does the file reading only do full files/lines/character
ex:
$file=fopen("text/dialogue.txt","r") or exit("unable to open dialogue file");
if($file == true) {
echo "File is open";
fgets($file);
$c = "";
while(!feof($file)) {
$line = fgets($file)
while($temp = fgetc($line)) {
$c = $c . $temp;
//if statement and comparrison
}
}
} else {
echo "File not open";
}
fclose($file);
You may use php file function to read a file line by line
<?php
$lines = file("myfile.txt");
foreach($lines as $line){
## do whatever you like here
echo($line);
}
?>
Please check php manual
http://php.net/manual/en/function.file.php

print contents of file until found the word hi

I want the program to print the document contents line by line while not reaching neither the end of file or found the word hi
The problem is when it found the word hi, it prints nothing although it is at position 22. Why not print the previous words how to solve this issue.
My file contain "Php is a special case hi. You will use less memory using the iterative solution. Moreover, function calls in PHP are costly, so it's better to avoid function calls when you can." string.
Here is my code
<?php
$contents = file_get_contents('m.txt');
$search_keyword = 'hi';
// check if word is there
$file=fopen("m.txt","r+");
while(!feof($file)&&strpos($contents, $search_keyword) == FALSE)
{
echo fgets($file)."<br>";
}
?>
change this condition
while(!feof($file)&&strpos($contents, $search_keyword) == FALSE)
to
while(!feof($file)) {
if(strpos($contents, $search_keyword) === FALSE) {
echo fgets($file)."<br>";
} else
break;
}
}
You mean print the file line by line until the word 'hi' is found?
<?php
$search_keyword = 'hi';
$handle = #fopen("m.txt", "r");
if ( $handle )
{
// Read file one line at a time
while ( ($buffer = fgets($handle, 4096)) !== false )
{
echo $buffer . '<br />';
if ( preg_match('/'.$search_keyword.'/i', $subject) )
break;
}
fclose($handle);
}
?>
You can replace the preg_match to strpos if you like.

Categories