Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
php advanced file editing
Old 05-19-2009, 10:39 PM php advanced file editing
Skilled Talker

Posts: 87
Trades: 0
Hi everyone! I'm pretty new at php (though not at programming; I know some Java, so all is good), so please help me out here!

I currently have a website for some writers. These writers would write stories/poems/etc and send them to me, and then I would create new html files and edit links on the site where necessary to "post" that story. Go here to see what I'm talking about.

The problem is, it takes me forever to add one of their stories because I have to first make and then edit their writing into html, and then edit links and stuff and it's just annoying.

So, I'm trying to make a php script. The problem is, everywhere i go on the web, tutorials talk about truncating a file, appending to a file, reading a file... but never "editing" a file. I know that since php is a such a powerful language, there must be a way to do this!

What I want is, say I have some code like:

Code:
<html>
<head></head>
<body>
<p>This is my first paragraph.</p>
<p>This is my second paragraph</p>
</body>
</html>
I want to be able to, using php, change the code to... say:

Code:
<html>
<head></head>
<body>
<p>This is my first paragraph. foo!</p>
<p>This is my second paragraph. bar!</p>
<p>This 3rd paragraph was added automagically by php.</p>
</body>
</html>
How would I accomplish this? many thanks!
__________________

Please login or register to view this content. Registration is FREE
- The most comprehensive online resource for students and educators.
yuechen is offline
Reply With Quote
View Public Profile Visit yuechen's homepage!
 
 
Register now for full access!
Old 05-20-2009, 03:12 AM Re: php advanced file editing
X-Cart's Avatar
Skilled Talker

Posts: 61
Trades: 0
Hello,

Overall, if you work with a file-based content, you simply need to open a file content via a variable, manipulate it as a string data and save a new version. That's it!

Details on file handling may be found at http://php.net/manual/en/book.filesystem.php

You may also read about string functions at http://php.net/manual/en/book.strings.php

Did it make things a bit clearer? Please, ask more questions should you need any help
__________________

Please login or register to view this content. Registration is FREE
shopping cart software
Our
Please login or register to view this content. Registration is FREE
on Facebook
X-Cart is offline
Reply With Quote
View Public Profile Visit X-Cart's homepage!
 
Old 05-20-2009, 03:58 AM Re: php advanced file editing
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
If you can be sure that the source is always clean, then the easiest / safest route is to load the file as a DOM document, and then use the DOM functions to add new nodes (paragraphs) and change nodes value before saving the DOM to a file.

You can go the string parsing way, but it's way more error prone.
http://www.php.net/manual/en/book.dom.php


To load html as a DOM document:
http://www.php.net/manual/en/domdocument.loadhtml.php
To add new paragraphs:
http://www.php.net/manual/en/domdocu...ateelement.php
To save the dom as html:
http://www.php.net/manual/en/domdocument.savehtml.php
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 05-20-2009 at 04:02 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 05-22-2009, 09:48 AM Re: php advanced file editing
markAtRitetek's Avatar
Experienced Talker

Posts: 32
Name: Mark Hudson
Location: Atlanta, GA
Trades: 0
http://us3.php.net/file_put_contents

Look at example 1; it's simple but it might be what you're looking for.
__________________
Mark Hudson
Ritetek Design

Please login or register to view this content. Registration is FREE
markAtRitetek is offline
Reply With Quote
View Public Profile Visit markAtRitetek's homepage!
 
Old 05-23-2009, 04:08 PM Re: php advanced file editing
Extreme Talker

Posts: 181
Name: David Jackson
Trades: 0
i would sugest looking into a CMS

Cant you store the stories in a database rather than manipulate a flat file?
__________________

Please login or register to view this content. Registration is FREE
davidj is offline
Reply With Quote
View Public Profile
 
Old 05-23-2009, 04:28 PM Re: php advanced file editing
Skilled Talker

Posts: 87
Trades: 0
A database is also very possible. It's just that, I haven't completely educated myself in php and mysql yet. :P

But the CMS I am very intrigued at. Do you know of a CMS that will perform what I'm trying to do?
__________________

Please login or register to view this content. Registration is FREE
- The most comprehensive online resource for students and educators.
yuechen is offline
Reply With Quote
View Public Profile Visit yuechen's homepage!
 
Old 05-24-2009, 03:24 AM Re: php advanced file editing
Extreme Talker

Posts: 181
Name: David Jackson
Trades: 0
check your pm
__________________

Please login or register to view this content. Registration is FREE
davidj is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to php advanced file editing
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.13320 seconds with 12 queries