Check if string exists in line of file
8 posts
Page 1 of 1
i want to open a file and check if a string the user inputs from a form (method post) and if the string exists in the file, remove it and then add it to a sql database.
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that
Code: Select all
Dim sr As New StreamReader("C:\File.txt")
Dim searchFor As String = "Hello"
Dim line As String = sr.ReadLine()
Do While line <> ""
If line.Contains(searchFor) Then
'Cant help you here dude :( No ideas about databases lol'
End If
line = sr.ReadLine()
Loop
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
MrAksel wrote:Im using php lol.Code: Select allDim sr As New StreamReader("C:\File.txt") Dim searchFor As String = "Hello" Dim line As String = sr.ReadLine() Do While line <> "" If line.Contains(searchFor) Then 'Cant help you here dude :( No ideas about databases lol' End If line = sr.ReadLine() Loop
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that
Ahh I didn't see what section. Well, no idea then
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
Well its been a few days, so can anyone help? and i would bump but there is no button to?
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that
I havent done any work with files in php but I found a few things for you that may help.
This opens a file and searches for a string and lets you know if it exists in the file:
This opens a file and searches for a string and lets you know if it exists in the file:
Code: Select all
I also found this which may do what you want but I havent tested it lol<?
$file = file_get_contents("filename.txt");
if(!strpos($file, "search for this")) {
echo "String not found!";
}
?>
Code: Select all
I guess you may be able to combine the 2 like://read the entire string
$str=implode("\n",file('filename.txt'));
$fp=fopen('somefile.txt','w');
//replace something in the file string - this is a VERY simple example
$str=str_replace('Find this','Replace with this',$str);
//now, TOTALLY rewrite the file
fwrite($fp,$str,strlen($str));
Code: Select all
Try and see lol :?<?
$file = file_get_contents("filename.ext");
if(strpos($file, "search for this")) {
//If string exists then remove it from file
//read the entire string
$str=implode("\n",file('somefile.txt'));
$fp=fopen('somefile.txt','w');
//replace something in the file string - this is a VERY simple example
$str=str_replace('Find this','Replace with this',$str);
//now, TOTALLY rewrite the file
fwrite($fp,$str,strlen($str));
//ADD YOUR OWN CODE FOR ADDING TO DATABASE HERE
}
?>
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
OH also for the post part..on html page:
Code: Select all
And you use $_POST["findthis"] to get the posted data..so in readfile.php your entire code would maybe be...<form action="readfile.php" method="post">
Search for: <input type="text" name="findthis" />
<input type="submit" />
</form>
Code: Select all
<?
$file = file_get_contents("filename.ext");
if(strpos($file, $_POST["findthis"])) {
//If string exists then remove it from file
//read the entire string
$str=implode("\n",file('somefile.txt'));
$fp=fopen('somefile.txt','w');
//replace something in the file string - this is a VERY simple example
$str=str_replace($_POST["findthis"],'Replace with this',$str);
//now, TOTALLY rewrite the file
fwrite($fp,$str,strlen($str));
//ADD YOUR OWN CODE FOR ADDING TO DATABASE HERE
}
?>
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
Hmm just saw this again, I'll check it in a second.
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that
8 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023