Forum Moderators: coopster

Message Too Old, No Replies

Regular Expressions: Save section of file to variable

Opened file with fopen(), need to find portion of text and save as variable

         

MatthewHSE

5:26 pm on Dec 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've got a file that I've opened with fopen(). I know the file is open because I'm able to edit it later on in my PHP script. The problem is, before I edit the file, I need to capture one section of the file and save it to a variable, and I can't quite figure out what needs to be done.

The file is just a plain .txt file, which will contain a link like the following:

<!--Start--><a href="/path/to/page.html" title="Title of Link"><!--End-->

I need to be able to use everything between the Start and End comments as a variable.

I've been working with regular expressions, but can't seem to make them work. Here's some relevant code:

$SSI = "/path/to/file.txt";
$Handle = fopen($SSI, 'w+');

How can I use regular expressions to capture the necessary portion of the $SSI file?

Salsa

5:56 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



Maybe this thread from a couple of weeks ago will help you:

extracting <title> contents from html files [webmasterworld.com]

MatthewHSE

8:11 pm on Dec 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe it would if I knew more about PHP, but as it stands, I can't seem to figure it out. My problem is centered around how to use the regular expressions, I guess, and that thread seems to address more about how to open directories.

Here's a bit more of what I'm trying to do:

$SSI = "/path/to/file.txt";
$Content = "Content to put in file";
$Handle = fopen($SSI, 'w+');
// The following line is what's causing the error:
$LastWeek = $Handle(ereg('(<!--Start-->)(.*)(<!--End-->)'));
$Content = "$Content $LastWeek";
$Write = fwrite($Handle, $Content))

The "$LastWeek" line is where I'm having a problem. I keep getting an error of "Fatal error: Call to undefined function: resource id #4() in /path/to/script.php on line 71" That sounds like I'm using the ereg function incorrectly, but I sure can't see a problem. Somehow I need to get all the content between those Start and End comments into a variable.

Thanks,

Matthew

Salsa

8:53 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



After you get your
$file
into a variable, try:

preg_match('#<!--Start-->(.*?)<!--End-->#', $file, $matches); 
$found_comment = $matches[1];

The # are just the boundaries for the match. You can use any character except special characters. Calling preg_match [us2.php.net] will set the array, $matches. Everthing between the #...# should be in $matches[0]. What's within the (.*) should be in $matches[1]. the dot represents any character. The * means any number of the previous characters. The '?' makes the pattern "ungreedy." What this means is that if you had additional <!--End--> substrings later in the file, the regex's greediness would attempt to match everything up to the last one. The '?' makes it stop at the first one.

I hope this helps

MatthewHSE

9:34 pm on Dec 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Getting closer, but still not there. It's giving me a message of, "Notice: Undefined offset: 1" for the $found_comment line. From a Google search I learned that this probably means the $match[1] variable has nothing in it. So, I looked around a bit at php.net, found the info on fread, and so I tried the following:

$SSI = "/path/to/file.txt";
$Handle = fopen($SSI, 'a+');
$Contents = fread($Handle, filesize($SSI));
preg_match('#<!--Start-->(.*?)<!--End-->#', $Contents, $match);
$found_comment = $match[1];
echo "<p>$found_comment</p>";

Unfortunately that's still giving me the same undefined offset error message. I checked to make sure the file has the <!--Start--> and <!--End--> comments in it, and it does. So somehow things still aren't reading what comes between those comments.

Salsa

11:34 pm on Dec 17, 2004 (gmt 0)

10+ Year Member



I copied and pasted the exact code from your last post and in the same directory as the script made a file.txt with the appropriate tags imbedded. It ran perfectly the first time. My first guess is that your path is off, and the file isn't even being opened. When debugging, it's always a good idea to do some error checking, like this:

$SSI = "file.txt"; 
if (!$Handle = fopen($SSI, 'a+')) die("Could not open file.");
if (!$Contents = fread($Handle, filesize($SSI))) die("Could not read file.");
if (!preg_match('#<!--Start-->(.*?)<!--End-->#', $Contents, $match)) die("preg_match found no match.");
if (!$found_comment = $match[1]) die("\$match[1] was not set.");
echo "<p>$found_comment</p>\n\n";