编辑/设置文件/数组中的文本的样式,并将其按顺序放回样式



tl;dr
我正在尝试从文本文件中设置特定行的样式,最终能够按原始顺序在页面上显示它。

信息:我的页面正在运行带有sourcerer扩展的Joomla
愿望清单的示例内容:

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

完整故事
我正试图为我的家人建立一个简单的网站,可以显示我们的愿望列表。我们目前正在dropbox上保存这些文件,到目前为止,我已经想好了如何让网站读取这些文件。

我希望脚本将以一个特殊字符开头的每一行都加粗,并将所有其他行放在项目符号列表中,但不是加粗的行。

奖励回合:如果文本中有任何链接,如果它们可以点击,那就太疯狂了。

问题
现在我做一个preg_match来检查是否有任何行包含特定的字符,如果包含,则将其放入一个数组中,如果不包含,则会将其放入另一个数组。我不知道如何对行进行检查,只对其中一些行应用样式,然后按正确的顺序返回,以便在页面上正确显示它们。

我的代码

<?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;
?>

我不知道为什么要将行移动到单独的数组中!只需一行一行地迭代,并计算出应该在哪里!

使用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

替代

我们没有使用ul | olHTML列表,我们可以将css中的列表属性添加到其他元素:

// 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>";
}
}