View Full Version : Finding Text in a File

06-01-2007, 10:30 PM
'I'm working on a script to open a txt file and load all the text after a particular line (wherever the word "BEGIN" is found). I've tried both array_search() and array_keys() (since file() returns the contents of a file as an array). I also tried converting the array to a string at each key and checking the string for the word, but that doesn't work either. Does anyone see the problem, or have any other suggestions?

/* Attempt to find BEGIN in file
echo $start_line;

// Try converting to a string each time...
foreach ($file_contents as $lines => $file_contents){
if ($found_code==false){
echo $file_contentss."<br />\n";
if ($file_contentss=='BEGIN'){
else {
echo htmlspecialchars($file_contents);
?>(Yeah, I took out array_search() and replaced it with array_keys().)

06-01-2007, 10:41 PM
you can find out if text is in an array with in_array ( http://uk3.php.net/function.in-array )

06-01-2007, 11:05 PM
OK, but I know it's there. I just need to find out where.

mr e
06-01-2007, 11:17 PM
Use strstr (http://www.php.net/manual/en/function.strstr.php) along with file_get_contents (http://www.php.net/manual/en/function.file-get-contents.php) so it's one string instead of an array

06-01-2007, 11:36 PM
I initially made it an array so I could add line breaks, but I'm putting it in a textarea, so I guess that doesn't matter.

And mr e, is there a way to do what you're saying, but then just take the first text after the "BEGIN"?

06-02-2007, 12:03 AM
Maybe that plus str_replace("BEGIN\n","",string); (assuming there's always only one line after "BEGIN"). But will the "\n" work?

06-02-2007, 12:11 AM
No, it doesn't. But adding line breaks directly to the PHP script does...

Final code:


echo '<textarea cols="100" rows="20">';
echo "\n";
echo $script;
echo "\n";
echo '</textarea>';


Now, for something slightly different, but on the same page:
How can I return everything between two strings?
For example, I have the whole file loaded into a string and I want to find text that's between two easily distinguishable "tags" (like "BEGIN text" and "END text").

mr e
06-02-2007, 12:55 AM
To convert new lines (\n) to breaks you can use nl2br (http://www.php.net/nl2br)

You could do str_replace('BEGIN', '', $string), then trim() (http://www.php.net/trim) to trim the beginning and ending whitespace (including \n)

For your last question, look into preg_match (http://www.php.net/preg_match) and regular expressions, here's an example

$string = 'BEGIN This is a string END';

preg_match('/BEGIN (.*) END/', $string, $matches);

echo $matches[1]; // Outputs This is a string

06-02-2007, 01:42 AM
I tried the preg_match and all I got was an empty array.

foreach ($funcarr as $incr => $funcarr){
preg_match('/BEGIN DOCUMENTATION (.*) END DOCUMENTATION/', $cont, $matches);
echo $matches[1]."<br /><br /><br /><br />";

This is one of the files I'm trying to get information from:


* Factorial
* By P. J.


<strong>factorial (Factorial)</strong><br /><br />
To use:<br />
Call factorial(n,inside), where n is an integer, and inside is left blank.<br />
For example:<br />
factorial(5) returns 120<br /><br />
The actual math function:<br />


function factorial(n,inside){
var orign=n;
while (n>1){
if (n=1){
if (!inside){

mr e
06-02-2007, 03:48 AM
Now you see how much fun regular expressions can be ;)

What you want is this

preg_match('/BEGIN DOCUMENTATION\s(.*)\sEND DOCUMENTATION/s', $string, $matches);

To explain it, you're looking for BEGIN DOCUMENTATION, then your not looking for a space, but in your example a newline, so I used \s which matches any whitespace character, then you're looking for anything (though the . by itself won't work), then another whitespace character, then END DOCUMENTATION

Then I have the s modifier on the end because by default . won't match newlines (which is why it wouldn't work just by itself), which we need it to

http://us.php.net/manual/en/reference.pcre.pattern.syntax.php is a great reference for regex syntax

06-02-2007, 04:30 AM
Thanks! It works perfectly now.

(and I just gave you some good rep :thumbsup: )

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum