Solved

PHP Script - How to manipulate text in file

Posted on 2011-02-25
9
268 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 38

Expert Comment

by:Aaron Tomosky
Comment Utility
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
Comment Utility
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
Comment Utility
thanks dtocci for the example
wondering how can i add an extra paragraph at the beginning of the file?
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
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
Comment Utility
$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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
HTML page and JavaScript 2 48
Mobile version of website does not react 6 49
Creating a slider 12 31
while loop in html mail format 5 31
Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now