LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 05-26-2011, 12:28 PM   #1
aocferreira
Member
 
Registered: Feb 2011
Posts: 110

Rep: Reputation: 0
PHP: Find a string in a text file and add something to that line


Hello,

I have a text file with something like this:
abc
def
ghi

I want to know how can I add something to a specific line.. the output would be something like:

abc
def 123
ghi

Search for string "def" and add something to that line.
Thank you
 
Old 05-26-2011, 02:52 PM   #2
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 198

Rep: Reputation: 30
Use file_get_contents to read the file and then preg_replace or str_replace to change the line. Finally save the new file with file_put_contents.
 
  


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
simple php script to add line/file to text file dnoy Programming 1 05-21-2008 06:08 PM
quick way to add string to a text file ufmale Linux - Newbie 1 03-11-2008 05:03 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 09:22 AM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 07:02 AM


All times are GMT -5. The time now is 07:33 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration