2

i want to put a .txt file in folder on Webserver.

Via PHP i want to get the content of the .txt file placed on the Startpage of a Webside.

The Text file i want to code with Placeholders, like this form

[Title]

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam

[BREAK]

[Headline]

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam

The Placeholders should generate the CSS or the HTML of the Text automatically, like [Title] = H1 or [Break] = < /br>

So my question, how can i do it?

1
  • 1
    sounds like your looking for a template engine. Commented Mar 10, 2014 at 18:24

3 Answers 3

1

you need to parse the contents of your file:

<?php
$tokens = array(
  'title'    => array('type' => 'multi-line', 'tag' => 'h1'),
  'break'    => array('type' => 'single',  'tag' => 'br'),
  'headline' => array('type' => 'single',  'tag' => 'hr')
);
$currentToken = null;
// Loop
foreach (file('input.txt') as $line) {
  if(strlen($line)==0)//empty case
    continue;
  //check tags
  if(preg_match('/\[(\w+)\]/', $line, $match)){
    if(isset($tokens[strtolower($match[1])])) {
      //multi-line case
      if($currentToken != null and $currentToken['type'] == 'multi-line') {
        echo "</{$currentToken['tag']}>"; //close multi-line Tag
      }
      $currentToken = $tokens[strtolower($match[1])];
      //single and multi-line
      echo ( $currentToken['type'] == 'single')?
        "\n<{$currentToken['tag']}/>": // print a single tag
        "<{$currentToken['tag']}>"     //open multiline tag
      ;
    }
  } else {
    echo $line;
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

To quote @13ruce1337, you seem to be looking for a template engine. There are many of those in PHP, the most common would be Twig and Smarty.

You could create your own, but it is a complex system, and there are many possibilities for errors, thus I wouldn't suggest building one for regular use until you are confident in your abilities. However, it is an extremely good learning exercise.

Comments

0

Try to learn some Regular Expressions and just write some code.

here is a little example, maybe too weak,but it's a good start:

<?php
$fp = fopen('/tmp/test.txt','r');
while(!feof($fp))
{
    $content = trim(fgets($fp,4096));
    if(!$content) continue;
    //see if this is a tag? if it is, set the tag name;
    if(preg_match('/\[(\w+)\]/', $content,$match))
    {
        $tagname = $match[1];
    }
    else//if it is not a tag,then its the content.
    {
        $tagname = $tagname ? $tagname : 'div';
        echo "<{$tagname}>{$content}</{$tagname}>\n";
    }
}

BUT! to invent a new markup languange is not a good idea,use HTML,or Markdown is a better solution.

if you just want to write html without writing tags, Markdown is a good choice;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.