How do you use preg_match to search for a string inside of a text file in PHP?
-
I tried to do this but it doesn't work. if (preg_match("/testFile.txt/",$a)) { echo "A match was found."; } else { echo "A match was not found."; } I'm looking to see if a is part of the text file but when i run it won't detect it and says match was not found.
-
Answer:
First of all, LEARN THE FREAKING PHP LANGUAGE! Secondly (this is where I'll help you), this is how you use the preg_match function: preg_match( 'regular expression', file_get_contents('filename') ); replace 'regular expression' with a regular expression of your choice. The Perl maunal has a great tutorial for how to use them in Perl, and most of the stuff applies to PHP to (although not everything does). replace 'filename' with the name of the file you want to read from.
Mar at Yahoo! Answers Visit the source
Other answers
First you have to load testFile.txt to a variable. I believe readfile is the simplest way to to do this. $var = readfile('testFile.txt'); if (preg_match('/some regex/',$var)) { echo "A match was found."; } else { echo "A match was not found."; }
Related Q & A:
- How to replace a string inside of a Word Document?Best solution by Stack Overflow
- How to read a PDF file with PHP?Best solution by Stack Overflow
- How can I convert a string number to a number in Perl?Best solution by Stack Overflow
- How do you use wildcards in search?Best solution by Google Answers
- How to search for a particular string in a text file using java?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.