Solved

PHP Script - How to manipulate text in file

Posted on 2011-02-25
9
276 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 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

20 Experts available now in Live!

Get 1:1 Help Now