note 49437 deleted from function.strlen by dave

From: Date: Fri, 05 May 2006 03:28:27 +0000
Subject: note 49437 deleted from function.strlen by dave
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-110604@lists.php.net to get a copy of this message
Note Submitter: corey at eyewantmedia dot com ---- I found that I needed something a little beyond array pushing and popping so I could share a stack between calls to a script. I wrote these quick functions, and while they are not all that efficient, they are certainly better than hitting a database server to manage a stack between script calls. Anyway, here we go. function FilePush($filename, $thingToPush, $terminator="\n") { $oldStuff = file_get_contents($filename); //Get the old file $result = file_put_contents ( $filename, $thingToPush . $terminator . $oldStuff ); //add the new stuff to the old and write it return $result; //return whatever file_put_contents says } function FilePop($filename, $terminator="\n") { $oldStuff = file_get_contents($filename); //get those file contents $pos = strpos($oldStuff, $terminator); // find our first terminator if ($pos == false) { //No terminator detected. Is there one item or no items? if (strlen($oldStuff) < 1) { // There is nothing! $result = false; } else { //There is only one thing! $result = $oldStuff; //store that thing file_put_contents ( $filename, ''); //pop it out of file } } else { //There are many things, and we found the first one $result = substr($oldStuff, 0, $pos); //get the first thing file_put_contents ( $filename, substr($oldStuff, ($pos + strlen($terminator))) ); //re-put everything but the popped item in the file. } return $result; //return whatever happened }

« previous php.notes (#110604) next »