LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-22-2003, 09:51 AM   #1
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Rep: Reputation: 30
Yep, another PHP question (File manipulation)


I need a tutorial or something on file manipulation. I need to be able to search for data values in a file and then overwrite them. I've been searching on php.net but have had no luck.

Any suggestions??
 
Old 11-22-2003, 11:11 AM   #2
hadding
Member
 
Registered: Oct 2003
Distribution: Redhat 9
Posts: 40

Rep: Reputation: 15
Simple search and replace:

$file = fopen($filename, 'r');

$file[$stringToMatch] = $newValue;



If you want to use regex's:


$file = fopen($filename, 'r');

foreach ($file as $key => $value) {

if (preg_match($pattern, $key)) {

$value = $newValue;

}

}


_h

What kind of data file is it. I mean what format? eg. A key data pair seperated by space on each line.
 
Old 11-22-2003, 11:12 AM   #3
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
I'm simply tab delimiting my file. Is that the best way to do it?

I'm not sure what your "regex" thing means???
 
Old 11-22-2003, 11:39 AM   #4
hadding
Member
 
Registered: Oct 2003
Distribution: Redhat 9
Posts: 40

Rep: Reputation: 15
regex == regular expressions

If its tab deleimited you'de use something like this

$find = "find";

$replace = "replace";

$filename = "/path/to/your/file.txt";



if ($fd = @fopen($filename, "r+")) {

$contents = ereg_replace("$find ([^\t]*)\n", "$find $replace\n", fread($fd, filesize($filename))."\n");

rewind($fd);

if (!@fwrite($fd, $contents)) {

echo "Error writing to file";

}

fclose($fd);

} else {

die("Could not open $filename for reading and writing");

}

Explanation:



read($fd, filesize($filename))."\n"); == Read the whole file into a string & add a newline at the end

ereg_replace("$find ([^\t]*)\n == search through that string for the pattern $find followed by a tab, followed by any number of caracters EXECEPT a new line, followed by a new line


_h

Last edited by hadding; 11-22-2003 at 11:41 AM.
 
Old 11-22-2003, 11:45 AM   #5
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
Ok thanks,
I'll work with that and let you know how it goes.
 
Old 11-22-2003, 11:50 AM   #6
hadding
Member
 
Registered: Oct 2003
Distribution: Redhat 9
Posts: 40

Rep: Reputation: 15
Here's a search and replace class that you might want to peruse

http://www.phpguru.org/search.replace.html

Blurb:

"Class to enable search/replace of files. Can perform the search over one file, multiple files, entire directories with/without subdirectories. Can search using four different search functions, supporting ereg and preg regular expressions."


[Edit]

If you don't know much about regular expressions I'd recemmend that you get ahold of a copy of "Mastering Regular Expressions" from O'Reilly

http://www.oreilly.com/catalog/regex/

It's a great book (still workin through it myself )

hope this helped

_h

Last edited by hadding; 11-22-2003 at 12:02 PM.
 
Old 11-22-2003, 01:07 PM   #7
jacksmash
Member
 
Registered: Nov 2003
Location: Ontario, Canada
Distribution: Ubuntu
Posts: 269

Original Poster
Rep: Reputation: 30
<started new thread>

Last edited by jacksmash; 11-22-2003 at 02:30 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script: File Manipulation simon_qwl Programming 2 10-29-2005 03:40 PM
file manipulation with c C.Aymen Programming 2 09-01-2005 12:48 PM
C File Manipulation in Linux drigz Programming 5 10-01-2004 07:28 AM
File name manipulation with tr, concatination troubles goofyheadedpunk Programming 9 07-06-2004 02:39 AM
Yep, another jre question! JMC Linux - Software 3 06-04-2002 10:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration