Skip to content
Advertisement

Editing/styling text from file/array and putting it back in order, styled?

tl;dr
I’m trying to style specific lines from a textfile, and in the end being able to show it on the page in the original order.

info: My page is running Joomla with the sourcerer extension.
Sample content of the wishlist:

wish 2
wish 3

§Category 1
    C1_wish1
    C1_wish2
    C1_wish3

§Category 2

    C2_wish1
    C2_wish2
    C2_wish3

wish 4 - link https://google.com
wish 5
wish 6

Full story
I’m trying to set up a simple site for my family that can display our wishlists. We’re currently keeping the files on dropbox and so far I’ve figured out how to make the site read the file.

I would like for the script to make every line that starts with a special character, bold, and all other lines to be put in a bullet-list, but not the bolded ones.

bonus round: If there’s any links in the text it would be insane if they could be clickable.

The Problem
Right now I do a preg_match to check if any lines contains a specific Character, and if it does it’s put in an array, if not, it’ll be put in another. I don’t know how to run the check on the lines, apply the styling only to some of them and then return them in the correct order so I can display them correctly on the page.

My code

<?php

header("Content-type: text/html");

$string = file_get_contents('https://dropboxlink/wishlist.txt');
$array = explode("n",$string);

foreach($array as $arr) 
{
 if(!(preg_match('#^-#', $arr))) 
   {
   $output[] = $arr;
   }

 else
   {
   $output2[] = $arr;
   }
}
$out = implode("n",$output);
$out2 = implode("n",$output2);

echo $out;
echo $out2;
?>

Advertisement

Answer

I’m not sure why you are moving the lines into separate arrays! Just iterate through line by line and work out what should be where!

Using preg_match

$array = explode("n", $text); // Split text into lines
$open_list = false;            // Set sub list flag to false
echo "<ul>";                   // Begin list

// Loop through each line of text
foreach($array as $line){

    // If the line is blank, skip it
    if(preg_match("/^s*$/", $line)){
        echo "<br>";
        continue;
    }

    // Check if the line is a category heading
    if(preg_match("/^§/", $line)){

        // Check to see if we're already on a sub list
        // if we are then close it and set flag to false
        if($open_list){
            echo "</ul>";
            $open_list = false;
        }

        $line = trim($line, "§");  // Trim the special character from the line
        echo "<li><b>{$line}</b></li><ul>";     // Print out the line and begin a sub list
        $open_list = true;                      // Set flag to true
        continue;                               // Skip rest of code block, we don't need it for a category
    }

    // We only get here IF the line isn't a category and isn't blank!

    // Check to see if the line starts with a non-white space character
    // and whether or not the sub list flag is set. If both conditions
    // are true then close the sub list and set flag to false
    if(preg_match("/^[S]/", $line) && $open_list){
        echo "</ul>";
        $open_list = false;
    }

    // Turn URLs into hyperlinks and print out the line
    $line = preg_replace("/([w:/.?=%]+.[w:/.?=%]+)/", "<a href="$1">$1</a>", $line);
    echo "<li>{$line}</li>";
}
echo "</ul>";  // Close list

Alternative

We don’t have to use the ul | ol HTML lists we can add list properties in css to other elements:

// Define styles for each element
$category  = "font-weight: bold;";
$sub_list  = "padding-left: 20px; display: list-item; list-style-type: circle; list-style-position: inside;";
$main_list = "padding-left: 10px; display: list-item; list-style-type: disc; list-style-position: inside;";

$text = "
wish 2
wish 3

§Category 1
    C1_wish1
    C1_wish2
    C1_wish3

§Category 2

    C2_wish1
    C2_wish2
    C2_wish3

wish 4 - link https://google.com
wish 5
wish 6
";

$array = explode("n", $text); // Split text into lines

// Loop through each line of text
foreach($array as $line){

    // Check if the line is blank
    if(preg_match("/^s*$/", $line)){
        echo "<br>";
    }

    // Check if the line is a category
    elseif(preg_match("/^§/", $line)){
        $line = trim($line, "§");
        echo "<div style="{$category}">{$line}</div>";
    }

    // Check if the line is a sub list
    elseif(preg_match("/^s+S+/", $line)){
        $line = trim($line, "s");
        $line = preg_replace("/([w:/.?=%]+.[w:/.?=%]+)/", "<a href="$1">$1</a>", $line);  // Replace URLs with hyperlinks
        echo "<div style="{$sub_list}">{$line}</div>";
    }

    // Otherwise it's a normal list item
    else{
        $line = preg_replace("/([w:/.?=%]+.[w:/.?=%]+)/", "<a href="$1">$1</a>", $line);   // Replace URLs with hyperlinks
        echo "<div style="{$main_list}">{$line}</div>";
    }
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement