...

View Full Version : Finding Text in a File



thesmart1
06-01-2007, 11: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?


<?php
$file_contents=file('functions/'.$_GET['funcname'].'.txt');
/* Attempt to find BEGIN in file
$line=array_keys($file_contents,'BEGIN');
print_r($file_contents);
$start_line=settype($line[0],'string');
$start_line=$start_line+2;
echo $start_line;
*/

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

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

thesmart1
06-02-2007, 12:05 AM
OK, but I know it's there. I just need to find out where.

mr e
06-02-2007, 12:17 AM
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

thesmart1
06-02-2007, 12:36 AM
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"?

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

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

Final code:

<?php
$cont=file_get_contents('functions/'.$_GET['funcname'].'.txt');
$scriptt=strstr($cont,'BEGIN');
$script=str_replace("BEGIN

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

Thanks!

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, 01: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

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


<?php
$funcarr=$_POST['function'];
foreach ($funcarr as $incr => $funcarr){
$cont=file_get_contents('functions/'.$funcarr.'.txt');
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:

functions/Factorial.txt

********************
*
* Factorial
* By P. J.
*
********************

REQUIRES <>

BEGIN DOCUMENTATION
<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 />
n!
END DOCUMENTATION

BEGIN SCRIPT

function factorial(n,inside){
var orign=n;
opern=n;
while (n>1){
n--;
opern*=n;
}
if (n=1){
if (!inside){
document.getElementById('mathfunc_factorial').innerHTML=opern;
}
}
}

mr e
06-02-2007, 04: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

thesmart1
06-02-2007, 05: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