SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display contents of a .txt file as HTML

    Hey guys,

    Built a simple site for a mate in a couple of days as a favour and now he wants to update a few lines of text now and then.

    The site was built using CSS and PHP, pretty simple really.

    I know there is a simple way of storing all the text content into an external .txt file and then including the .txt file into the site via a php include. This is fine, but the problem is that the text is always included without any HTML formatting such as without the appropriate heading, paragraph etc tags.

    So instead of the text from the .txt file being displayed as:

    Code:
    <h1>Heading 1</h1>
    
    <p>Some lovely text</p>
    It is displayed simply as:

    Code:
    Heading 1
    
    Some lovely text
    In a nutshell, I'm just looking for a super-simple way for my mate to update a few text areas of his site by simply updating the relevant .txt file.

    Cheers in advance,

    Darren

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Though i am not clearly understanding all what exactly you want but if you store the text in a .txt file without any formatting, then it is difficult to determine PHP on loading what text is to be inside which tag. So you need to think about having some CMS (content management systems). Google for some CMS or try to learn PHP and Database (MySQL) and store the formatter texts in the database and pull everything from there.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My friend won't be updating his site often, but the parts that he does want to update are only a few lines of text so a full-blown CMS would be overkill for something as simple as this, especially since I only made the website as a favour to him. I was hoping there may have been a simpler solution that wouldn't require too much time or effort on my part, as I'm not being paid anything.

    I guess you could build a very simple parser and define a syntax that the client could use.

    eg, bbcode style:

    Code:
    [h1]heading[/h1]
    
    [p]some lovely text[/p]
    and then, in the simplest form, use str_replace to convert these to html tags.

    Code:
    $source = file_get_contents('/path/to/file.txt');
    
    $source = str_replace("[", "<", $source); // convert [ to <
    $source = str_replace("]", ">", $source); // convert ] to >
    
    echo $source;
    This works well and does what I want, but it requires extra effort on the client's part. Is there not a simpler solution?

    Cheers,

    Darren

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The term you might be seeking is: flat file database, might throw up a few ideas.

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Perhaps you could introduce the idea of Markdown to your client.
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that, guys. Doing a bit of the old research into flat file databases just now.

    And this Markdown thing looks promising.

    Thanks again,

    Darren


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •