Editing/styling 来自 file/array 的文本并将其按顺序、样式化?
Editing/styling text from file/array and putting it back in order, styled?
tl;dr
我正在尝试为文本文件中的特定行设置样式,并最终能够以原始顺序在页面上显示它。
信息: 我的页面是 运行ning Joomla,带有 sourcerer 扩展。
愿望清单的示例内容:
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
全文
我正在尝试为我的家人建立一个可以显示我们的愿望清单的简单网站。
我们目前将文件保存在保管箱中,到目前为止,我已经想出了如何让网站读取文件的方法。
我希望脚本将以特殊字符开头的每一行加粗,并将所有其他行放在项目符号列表中,但不包括加粗的行。
奖金回合:如果文本中有任何链接,如果它们可以点击,那就太疯狂了。
问题
现在我执行 preg_match 检查是否有任何行包含特定字符,如果包含,则将其放入数组中,否则将放入另一个数组中。
我不知道如何 运行 检查行,只对其中的一些应用样式,然后 return 按正确的顺序排列它们,这样我就可以在页面上正确显示它们。
我的代码
<?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=\"\"></a>", $line);
echo "<li>{$line}</li>";
}
echo "</ul>"; // Close list
备选
我们 没有 使用 ul | ol
HTML 列表我们可以将 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=\"\"></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=\"\"></a>", $line); // Replace URLs with hyperlinks
echo "<div style=\"{$main_list}\">{$line}</div>";
}
}
tl;dr
我正在尝试为文本文件中的特定行设置样式,并最终能够以原始顺序在页面上显示它。
信息: 我的页面是 运行ning Joomla,带有 sourcerer 扩展。
愿望清单的示例内容:
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
全文
我正在尝试为我的家人建立一个可以显示我们的愿望清单的简单网站。
我们目前将文件保存在保管箱中,到目前为止,我已经想出了如何让网站读取文件的方法。
我希望脚本将以特殊字符开头的每一行加粗,并将所有其他行放在项目符号列表中,但不包括加粗的行。
奖金回合:如果文本中有任何链接,如果它们可以点击,那就太疯狂了。
问题
现在我执行 preg_match 检查是否有任何行包含特定字符,如果包含,则将其放入数组中,否则将放入另一个数组中。
我不知道如何 运行 检查行,只对其中的一些应用样式,然后 return 按正确的顺序排列它们,这样我就可以在页面上正确显示它们。
我的代码
<?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=\"\"></a>", $line);
echo "<li>{$line}</li>";
}
echo "</ul>"; // Close list
备选
我们 没有 使用 ul | ol
HTML 列表我们可以将 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=\"\"></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=\"\"></a>", $line); // Replace URLs with hyperlinks
echo "<div style=\"{$main_list}\">{$line}</div>";
}
}