Welcome to MobyThreads.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in
All support for the MobyThreads Threaded phpBB MOD can now be found on welsolutions at this forum

php + txt

 
   Web Hosting and Web Master Forums (Home) -> Webmaster RSS
Next:  Free Rich Text Editor.. any others?  
Author Message
kreten1983

External


Since: Nov 21, 2003
Posts: 8



(Msg. 1) Posted: Sat Nov 22, 2003 2:03 am
Post subject: php + txt
Archived from groups: alt>www>webmaster (more info?)

i need to write php script thats gonna change some TXT file.. in text file i
have this:

something=
blah blah blah


i need to write something after "something="


for example... i need to get this:

something=
aaaaaaa aaaaaaa aaaaaaaaa aaaaaaa aaaaaaaa
blah blah blah



i know how to add something on the end of the file but i dont know how to
place it between "something=" and "blah blah blah"

 >> Stay informed about: php + txt 
Back to top
Login to vote
sp_bhuisman

External


Since: Jun 27, 2003
Posts: 571



(Msg. 2) Posted: Sat Nov 22, 2003 2:04 am
Post subject: Re: php + txt [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Fri, 21 Nov 2003 23:03:59 +0100, Platypus <kreten1983 RemoveThis @yahoo.com> wrote:

 > i need to write php script thats gonna change some TXT file.. in text
 > file i
 > have this:
 >
 > something=
 > blah blah blah
 >
 >
 > i need to write something after "something="
 >
 >
 > for example... i need to get this:
 >
 > something=
 > aaaaaaa aaaaaaa aaaaaaaaa aaaaaaa aaaaaaaa
 > blah blah blah
 >
 >
 >
 > i know how to add something on the end of the file but i dont know how to
 > place it between "something=" and "blah blah blah"

$incomingFileText = read_file(... yada yada);

$replaceStr = "aaaaaaa aaaaaaa aaaaaaaaa aaaaaaa aaaaaaaa";
$newContent = preg_replace("/something=/", "something=\n".$replaceStr,
$incomingFileText);

Grey

--
The technical axiom that nothing is impossible sinisterly implies the
pitfall corollory that nothing is ridiculous.
- <a style='text-decoration: underline;' href="http://www.greywyvern.com" target="_blank">http://www.greywyvern.com</a> - Orca RingMaker: PHP web ring creation and
management<!-- ~MESSAGE_AFTER~ -->

 >> Stay informed about: php + txt 
Back to top
Login to vote
spam3

External


Since: Jul 01, 2003
Posts: 411



(Msg. 3) Posted: Sat Nov 22, 2003 2:05 am
Post subject: Re: php + txt [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Platypus wrote:

 > i need to write php script thats gonna change some TXT file.. in text file i
 > have this:
 >
 > something=
 > blah blah blah
 >
 > i need to write something after "something="
 > for example... i need to get this:
 >
 > something=
 > aaaaaaa aaaaaaa aaaaaaaaa aaaaaaa aaaaaaaa
 > blah blah blah
 >
 > i know how to add something on the end of the file but i dont know how to
 > place it between "something=" and "blah blah blah"

<?php
$str='This is the new line!'."\n";
$file=dirname(__FILE__).'/Edit2.txt';
$contents=file($file);
foreach($contents as $id=>$line)
if(preg_match('/^something=(\r\n|\r|\n)$/',$line))
break;
$ar_end=array_splice($contents,$id+1);
$contents[]=$str;
$contents=array_merge($contents,$ar_end);
$fp=fopen($file,'w');
foreach($contents as $line)
fwrite($fp,$line,strlen($line));
fclose($fp);
?>

This will make sure that the the full line is matched, not just part of
it (in case you have "someting=" in another line as well). Not as
graceful as a simple:

preg_replace('/^(something=(\r\n|\r|\n))$/',"$1\n$str",$subject);

But I needed this exact functionality for something else. (I stripped
all the error-checking stuff to avoid confusion.)

--
Justin Koivisto - spam.RemoveThis@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.<!-- ~MESSAGE_AFTER~ -->
 >> Stay informed about: php + txt 
Back to top
Login to vote
Display posts from previous:   
   Web Hosting and Web Master Forums (Home) -> Webmaster All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]