note 37577 modified in function.strpos by aidan

From: Date: Thu, 10 Jun 2004 06:59:08 +0000
Subject: note 37577 modified in function.strpos by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-70814@lists.php.net to get a copy of this message
enough to search and replace a new string into a text file. hope this can save your time :-) original config.txt file: Current Background = [wallpaper/WinXP.jpg] Last Time = [20] <?php $var = file_get_contents("data/config.txt"); $out = fopen("data/config.txt", "w"); $var2 = writeConfig($var , "Current Background = [" , "]" , "wallpaper/Bgred.jpg"); $var2 = writeConfig($var2, "Last Time = [" , "]" , "21"); fwrite($out, $var2); fclose($out); function writeConfig($var, $begString, $endString, $newString) { $begPos = strpos($var,$begString) +strlen($begString); $endPos = strpos($var , $endString , $begPos); $result = substr($var, 0, $begPos); $result .= $newString; $result .= substr($var, $endPos); return $result; } ?> changed config.txt file: Current Background = [wallpaper/Bgred.jpg] Last Time = [21] --was-- // enough to search and replace a new string into a text file. // hope this can save your time :-) /* original config.txt file Current Background = [wallpaper/WinXP.jpg] Last Time = [20] */ <?php $var = file_get_contents("data/config.txt"); $out = fopen("data/config.txt", "w"); $var2 = writeConfig($var , "Current Background = [" , "]" , "wallpaper/Bgred.jpg"); $var2 = writeConfig($var2, "Last Time = [" , "]" , "21"); fwrite($out, $var2); fclose($out); function writeConfig($var, $begString, $endString, $newString) { $begPos = strpos($var,$begString) +strlen($begString); $endPos = strpos($var , $endString , $begPos); $result = substr($var, 0, $begPos); $result .= $newString; $result .= substr($var, $endPos); return $result; } ?> /* changed config.txt file Current Background = [wallpaper/Bgred.jpg] Last Time = [21] */ http://www.php.net/manual/en/function.strpos.php

« previous php.notes (#70814) next »