joel2k8 Posted March 16, 2011 Share Posted March 16, 2011 Hello, Okay for some reason i cannot do this, when i open a file in php and search for string, it doesn't seem to work, some codes i tried: $filename = 'example.txt'; $searchfor = 'hello'; $fh = fopen($filename, 'r'); $olddata = fread($fh, filesize($filename)); if(strpos($olddata, $searchfor)) { //fount it } else { //can't find it } fclose($fh); (above it just sample text, i was hoping you could give me sample code that will work) I tried many others like strstr, stristr, preg_replace etc etc, doesn't seem to find it.. this is my text file: sdfsdfsdfsdfsdf[b]hello[/b]sdvsdf dfadfsdfsdfsddfsdf sdfsdfsdfs sdffffffffffffffffffffdv vsvdsdsf I don't want to find it by line, i want to simply open the text file and find the string.. thanks.. Quote Link to comment https://forums.phpfreaks.com/topic/230852-search-for-string-in-a-text-file/ Share on other sites More sharing options...
Maq Posted March 16, 2011 Share Posted March 16, 2011 Do you just want to see if the string exists in the file? If so, you would use something like: $filename = 'example.txt'; $searchfor = 'hello'; $file = file_get_contents($filename); if(strpos($file, $searchfor)) { echo "String found"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/230852-search-for-string-in-a-text-file/#findComment-1188377 Share on other sites More sharing options...
johnny86 Posted March 16, 2011 Share Posted March 16, 2011 <?php $data = file_get_contents('file.txt'); if(strpos($data, 'findThis') !== FALSE) { // found 'findThis' } else { // Did not find 'findThis' } ?> I see Maq already posted almost identical code now that I clicked post.. But use !== FALSE because strpos could return 0 if the string found starts at offset 0 and that will result in a false in the if statement and would therefore give wrong result: if(0 == TRUE) // FALSE Quote Link to comment https://forums.phpfreaks.com/topic/230852-search-for-string-in-a-text-file/#findComment-1188379 Share on other sites More sharing options...
joel2k8 Posted March 16, 2011 Author Share Posted March 16, 2011 Yes, i just want to see the string exist and if does do something.. though the code you gave me doesnt seem to work, but it is a php file that i am getting the information from: test.php <a href="http://google.com/">Google</a> bhjb njkm, knklmnnkl hello <a href="#">test</a> <a href="http://google.com/">Google</a> <a href="http://google.com/">Google</a> <a href="http://google.com/">Google</a> then i try and check to if it exists by using: $old = 'hello'; $filename = 'test.php'; $file = file_get_contents($filename); if(strpos($file, $old)) { //string exists } it doesn't seem to work, does it have to be a txt file? i can write to the php file but can't seem to find the string.. thanks Quote Link to comment https://forums.phpfreaks.com/topic/230852-search-for-string-in-a-text-file/#findComment-1188384 Share on other sites More sharing options...
joel2k8 Posted March 16, 2011 Author Share Posted March 16, 2011 Hmm.. nevermind it was me using getting text from an XML file and using it as the string to search.. still don't understand why that would stop it from working.. thanks for helping anyway! Quote Link to comment https://forums.phpfreaks.com/topic/230852-search-for-string-in-a-text-file/#findComment-1188386 Share on other sites More sharing options...
Maq Posted March 16, 2011 Share Posted March 16, 2011 Hmm.. nevermind it was me using getting text from an XML file and using it as the string to search.. still don't understand why that would stop it from working.. thanks for helping anyway! If you still can't get it to work, posting example data would help. Quote Link to comment https://forums.phpfreaks.com/topic/230852-search-for-string-in-a-text-file/#findComment-1188389 Share on other sites More sharing options...
joel2k8 Posted March 17, 2011 Author Share Posted March 17, 2011 Hmm.. nevermind it was me using getting text from an XML file and using it as the string to search.. still don't understand why that would stop it from working.. thanks for helping anyway! I got it to work, before the code was: strpos($filecont, $name) I changed to: strpos($filecont, "$name") Now it works how it should, but for some reason i would of thought it would work with out the " ". Nevermind, thanks for the help, problem is now soved! If you still can't get it to work, posting example data would help. Quote Link to comment https://forums.phpfreaks.com/topic/230852-search-for-string-in-a-text-file/#findComment-1188465 Share on other sites More sharing options...
Maq Posted March 17, 2011 Share Posted March 17, 2011 So this is solved? Quote Link to comment https://forums.phpfreaks.com/topic/230852-search-for-string-in-a-text-file/#findComment-1188667 Share on other sites More sharing options...
joel2k8 Posted March 17, 2011 Author Share Posted March 17, 2011 Yeah.. the problem is solved, as i said in my last reply, i accidently put my text in the quote. thanks again for the help. Quote Link to comment https://forums.phpfreaks.com/topic/230852-search-for-string-in-a-text-file/#findComment-1188720 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.