We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!

Answered Php/Snippet Help

    • 42082
    • 72 Posts
    Hey guys, Modxnooby here again. I just needed some help with a snippet I am creating for a client I am currently working for. Basically my client wants me to create an online changelog editor/reader for a video game they are working on. They want to have the ability to upload there changelog from a local workstation to the server and have the changes display on the front end. Now the file reading I could handle if that was the only thing required, but in this situation they want to have inline tags converted to html tags serverside. For example the changelog might read:
    ~This is a heading~
    |this is a paragraph|
    The server needs to search the config line by line and replace each instance of '~' and '|'. So the result would look something like this:
    <h2>This is a heading</h1>
    <p>This is a paragraph</p>
    Now, this I have done with regular expressions before. But to make things work perfectly, the client also wants to have tags span multiple lines. For instance:
    ~This
    is
    a
    heading~
    Will change to:
    <h2>This is a heading</h2>
    I come to you all for advice on what i am doing wrong with my code.
    Here's my snippet:
    <?php
    $temp='';
    $filePath = '*path to the file in my file system*';
    $fileOpen = fopen($filePath,"r")or die("error");
    $pattern = '/^~(.*)~$/';
    $replacement = '<h2>\1</h2>';
    if (file_exists($filePath)) {
        while (!feof($fileOpen)) {
            $currline=$temp.fgets($fileOpen);
            if (preg_match("/^~(.*)~$/", $currline)) {
               echo preg_replace($pattern, $replacement,$currline);
              
            }else{
                 $temp = $currline;
            }  
        }
    }else {
        echo "The file $filePath does not exist";
    }
       
    

    This question has been answered by modxnooby. See the first response.

    [ed. note: modxnooby last edited this post 12 years, 1 month ago.]
    • discuss.answer
      • 42082
      • 72 Posts
      Hey everyone I was able to find a solution with the help of an experienced programmer. So thanks everyone.