Solved

PHP Script - How to manipulate text in file

Posted on 2011-02-25
9
302 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

696 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