Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为HTML/CSS呈现准备来自不可变资源的未完全格式化的文本_Php_Html_Css_Regex_User Interface - Fatal编程技术网

Php 为HTML/CSS呈现准备来自不可变资源的未完全格式化的文本

Php 为HTML/CSS呈现准备来自不可变资源的未完全格式化的文本,php,html,css,regex,user-interface,Php,Html,Css,Regex,User Interface,问题: 我们数据库中的多个文本字段–可能–包含用于 显示用比例字体书写的ASCII艺术或图形 它们包含与段落长度相同的行(我们要将其包装) 无法更改数据库中的字段 问题: 包装将破坏预期的“ASCII艺术” 使用空白进行渲染:预处理;溢出-x:自动CSS将导致很长的行,并应用非常不友好的水平滚动 我们使用与用户提交此内容时不同的字体进行呈现;因此,我们无法确定渲染是否准确 拟采用的解决方案: 在后端使用PHP,在前端使用HTML/CSS,而不更改源数据的情况下,尽可能按比例呈现预先格式

问题:

  • 我们数据库中的多个文本字段–可能–包含用于 显示用比例字体书写的ASCII艺术或图形
  • 它们包含与段落长度相同的行(我们要将其包装)
  • 无法更改数据库中的字段
问题:

  • 包装将破坏预期的“ASCII艺术”
  • 使用
    空白进行渲染:预处理;溢出-x:自动CSS将导致很长的行,并应用非常不友好的水平滚动
  • 我们使用与用户提交此内容时不同的字体进行呈现;因此,我们无法确定渲染是否准确
拟采用的解决方案:

  • 在后端使用PHP,在前端使用HTML/CSS,而不更改源数据的情况下,尽可能按比例呈现预先格式化的文本
问题:

  • 有没有更好的方法以非精心设计的方式解决这个问题,接受我们的输入是
    falsy
$myPreFormattedText

##IgnorantB----dGraph with a very long single-line paragraph

"Anim Muji labore Marylebone concierge qui et eu, dolor culpa proident joy est elegant. Sharp perfect eiusmod Toto soft power excepteur voluptate business class quality of life enim ea. Flat white craftsmanship exquisite exercitation conversation. Washlet destination nostrud, sint aute bulletin elegant. Bureaux uniforms flat white Gaggenau occaecat Ettinger Porter enim dolore bespoke Fast Lane artisanal in. Airport Melbourne non eu magna vibrant first-class the highest quality id alluring in Ettinger. Anim handsome enim Boeing 787, Singapore Winkreative concierge in smart exquisite Zürich sed do.

IgnorantB----dGraph:
11|
10|
9  |
8  |
7  |
6  |
5  |
4  |
3  |
2  |
1  |
    |__________________________________________
        |   1   |   2   |   3   |  4   |   5   |   6   |   7   |   8


Tadah, tadah, I'm so happy with myself
I MiSs Access to this application 2 much...
以下是我到目前为止的想法:

PHP:

HTML:



我希望能有所帮助,但还有更多需要改进的地方。

您可以使用一些算法逐行读取文本并对其执行一些简单的检查:

  • 如果行以空格开头-ASCII艺术

    (文本很少以空格开头。)

  • 如果行包含三(两)个或更多连续空格-ASCII艺术

    (文本不应包含≥3个连续空格或制表符。)

  • 该行仅包含
    [a-zA-Z0-9.,?!]
    和空白-文本

  • 否则-ASCII艺术

我不包含任何代码,因为您可能需要添加更多条件或修改它们,以便更好地处理您的输入


将其输出到系列


Text
您可以使用某种算法逐行读取文本并对其执行一些简单检查:

  • 如果行以空格开头-ASCII艺术

    (文本很少以空格开头。)

  • 如果行包含三(两)个或更多连续空格-ASCII艺术

    (文本不应包含≥3个连续空格或制表符。)

  • 该行仅包含
    [a-zA-Z0-9.,?!]
    和空白-文本

  • 否则-ASCII艺术

我不包含任何代码,因为您可能需要添加更多条件或修改它们,以便更好地处理您的输入


将其输出到系列


Text
我已经完成了问题的完美编辑;-)但我现在意识到,我的解决方案的真正问题是,一个图形化的字符行可能包含单间隔的“单词”(…),那么包装将——尽管如此——破坏图形(或者不会?)。我已经完成了问题的完美编辑;-)但我现在意识到,我的解决方案的真正问题是,一个图形化的字符行可能包含单间隔的“单词”(…),那么包装将——尽管如此——破坏图形(或者不会?),感谢您的输入。当然,这也说明了我没有看到的东西。“我会回来的”;-)谢谢你的意见。当然,这也说明了我没有看到的东西。“我会回来的”;-)
/**
 * Use this function to render fields that -could- contain graphs or ASCII-art to HTML.
 *   If input was created with a proportional font, render output the same, to come as close as it gets.
 * @param $input
 * @return string
 */
function reformatPreFormattedText($input)
{
    if (is_string($input)) {
        // New-line to <br />, so we do not need `white-space: pre;` in CSS
        $output = nl2br($input);
        // Replace `n` consecutive spaces with 1 space and n-1 `&nbsp;`
        $output = preg_replace('~(?<=\s)\s~', '&nbsp;', $output);
        // Replace ` &nbsp;` with `&nbsp;&nbsp;`
        $output = preg_replace('~ &nbsp;~', '&nbsp;&nbsp;', $output);

        return strval($output);
    }

    // If a logger is present, you might want to log an error here.
    return '';
}
pre-formatted-text {
    display: block;
    //   These fields sometimes contain graphs or ASCII-art that was created with proportional font.
    //   We choose the intention to render them as close as it gets, not using mono-space fonts.
    //   @see reformatPreFormattedText()
    //font-family: monospace;

    // Apply horizontal scroll to facilitate graphs that are too wide for the container
    overflow-x: auto;
  }
<pre-formatted-text>
    <?php echo reformatPreFormattedText($myPreFormattedText); ?>
</pre-formatted-text>