Solved

PHP Script - How to manipulate text in file

Posted on 2011-02-25
9
300 Views
Last Modified: 2012-06-27
Hi experts,

i am looking for a PHP script which reads a particular text file on the server, then replace certain text in the file.
For example, if there is sentence "man on the moon" found in the file, the script will replace it with "man on the earth".

and also to append a paragraph at the beginning of the file.

anyone can share some clue?

thanks!!
0
Comment
Question by:aska1979
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 39

Expert Comment

by:Aaron Tomosky
ID: 34986067
The general method is use fget or fopen and read the file into a variable, use pregreplace to change out strings, add what you want, then save it again. I'll let someone else give you a working example if that's what you're looking for.
0
 
LVL 3

Expert Comment

by:dtocci
ID: 34986068
something like this:

$path = ""; // file path here
$filestring = file_get_contents($path);
$filestring = str_replace("man on the moon","man on the earth",$filestring);
file_put_contents($path,$filestring);

Open in new window


make sure you have permission to read and write to that directory and file.
0
 

Author Comment

by:aska1979
ID: 34987145
thanks dtocci for the example
wondering how can i add an extra paragraph at the beginning of the file?
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34987705
Try

<?php

$path = ""; // file path here
$filestring = file_get_contents($path);
$filestring = str_replace("man on the moon","man on the earth",$filestring);
file_put_contents($path,$filestring);

$file = file($path);
$result = "";

foreach($file as $line => $text){
    $text = "\t".$text;
    $result = $result.$text;
}
    
file_put_contents($path,$result);

?>

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34987720
Oh, misunderstood - this was supposed to be the beginning of the file not each line, try this then:

<?php

$path = "test.txt"; // file path here
$filestring = file_get_contents($path);
$filestring = str_replace("man on the moon","man on the earth",$filestring);
file_put_contents($path,"\t".$filestring);

?>

Open in new window

0
 
LVL 3

Expert Comment

by:dtocci
ID: 34987766
$path = ""; // file path here
$filestring = file_get_contents($path);
$filestring = str_replace("man on the moon","man on the earth",$filestring);
$newparagraph = <<<NEWPARAGRAPH
<p>Put your new paragraph here...</p>
NEWPARAGRAPH;
$filestring = $newparagraph . $filestring;
file_put_contents($path,$filestring);

Open in new window

0
 
LVL 3

Expert Comment

by:dtocci
ID: 34987812
Actually if your document is a text file, that new paragraph probably shouldn't be wrapped in <p> tags, so you can leave those out.

If it's an HTML file, you may want to target the beginning of the body for your paragraph.  So instead of:

$filestring = $newparagraph . $filestring;

you'd use something like

$filestring = str_replace("<body>","<body>" . $newparagraph,$filestring);

That should do it.  Note that if you have event handlers in your body tag (like onload) you'll have to use preg_replace with regex for that replacement instead of str_replace.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34987952
This seems to be a text file though, as the author indicated in the question.
I do not think that there are any html tags.
0
 
LVL 3

Accepted Solution

by:
dtocci earned 500 total points
ID: 34987988
right. it's a text file. so it should be something like this

$path = ""; // file path here
$filestring = file_get_contents($path);
$filestring = str_replace("man on the moon","man on the earth",$filestring);
$newparagraph = <<<NEWPARAGRAPH
Put your new paragraph here...

NEWPARAGRAPH;
$filestring = $newparagraph . $filestring;
file_put_contents($path,$filestring);

Open in new window

0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Find out what you should include to make the best professional email signature for your organization.
Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question