?
Solved

PHP Script - How to manipulate text in file

Posted on 2011-02-25
9
Medium Priority
?
314 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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
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).
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

807 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