Check if string exists in line of file

8 posts Page 1 of 1
Contributors
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1891
Joined: Wed Dec 16, 2009 9:56 pm

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 :)
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

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!

Image
Image
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1891
Joined: Wed Dec 16, 2009 9:56 pm

MrAksel wrote:
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
Im using php lol.
Nailing my feet to the floor is easier than using my own muscles to balance, but you don't see me doing that :)
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

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!

Image
Image
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1891
Joined: Wed Dec 16, 2009 9:56 pm

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 :)
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

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:
Code: Select all
<?
$file = file_get_contents("filename.txt");
if(!strpos($file, "search for this")) {
echo "String not found!";
}
?>
I also found this which may do what you want but I havent tested it lol
Code: Select all
//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));
I guess you may be able to combine the 2 like:
Code: Select all
<?
$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

}
?>
Try and see lol :?
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

OH also for the post part..on html page:
Code: Select all
<form action="readfile.php" method="post">
Search for: <input type="text" name="findthis" />
<input type="submit" />
</form>
And you use $_POST["findthis"] to get the posted data..so in readfile.php your entire code would maybe be...

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.
User avatar
zachman61
VIP - Donator
VIP - Donator
Posts: 1891
Joined: Wed Dec 16, 2009 9:56 pm

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
Return to “Help & Support”