【问题标题】:Paragraph style in PHP stringPHP字符串中的段落样式
【发布时间】:2016-06-03 23:11:32
【问题描述】:

如果我喜欢那个格式字符串,我如何用 PHP 把它变成一个格式良好的段落,比如 text-align: justify in CSS and full-width of browser window?

<?php

$text = "It is a long 
established fact that a reader 
will be distracted 
by the readable content of a 
page when looking at its layout. 
The point of using Lorem Ipsum 
is that it has a 
more-or-less normal distribution of 
letters, as opposed to using 'Content here, 
content here', making it look like readable English. 
Many desktop publishing packages and web 
page editors now use Lorem Ipsum as their 
default model text, and a search for 'lorem ipsum' 
will uncover many web sites still in their infancy. 
Various versions have evolved over the years, 
sometimes by accident, sometimes on 
purpose (injected humour and the like).
";
?>

结果我想把这一段变成这种格式:

一个由来已久的事实是,读者在查看页面布局时会被页面的可读内容分散注意力。使用 Lorem Ipsum 的关键在于它具有或多或少的正态分布字母,而不是使用“这里的内容,这里的内容”,使它看起来像可读的英语。许多桌面出版包和网页编辑器现在使用 Lorem Ipsum 作为他们的默认模型文本,搜索“lorem ipsum”将发现许多仍处于起步阶段的网站。多年来,各种版本不断演变,有时是偶然的,有时是故意的(注入幽默之类的)。

【问题讨论】:

  • 从 $text 中删除 /n/r 字符(它们在您的第一个文本中不可见。)

标签: php


【解决方案1】:

就文本的格式化和显示而言,PHP 处理的很少。当您将上面的文本存储到那里的 $text 对象中时,直接将其输出到浏览器窗口将显示它,就像您在下面一样。您需要使用 CSS 和 HTML 来格式化您从 PHP 输出的文本。

此外,您在将它们分配给 $text 对象时看到的所有这些换行符都不会被 PHP 保留。您必须在存储文本字符串时明确声明需要换行。

【讨论】:

    【解决方案2】:

    将变量包装在&lt;p&gt; 标记中并应用样式

    <?php
    
    $text = "It is a long 
    established fact that a reader 
    will be distracted 
    by the readable content of a 
    page when looking at its layout. 
    The point of using Lorem Ipsum 
    is that it has a 
    more-or-less normal distribution of 
    letters, as opposed to using 'Content here, 
    content here', making it look like readable English. 
    Many desktop publishing packages and web 
    page editors now use Lorem Ipsum as their 
    default model text, and a search for 'lorem ipsum' 
    will uncover many web sites still in their infancy. 
    Various versions have evolved over the years, 
    sometimes by accident, sometimes on 
    purpose (injected humour and the like).
    ";
    
    $text = "<p style='text-align:justify'>$text</p>";
    
    echo $text;
    

    <p style='text-align:justify'>It is a long 
    established fact that a reader 
    will be distracted 
    by the readable content of a 
    page when looking at its layout. 
    The point of using Lorem Ipsum 
    is that it has a 
    more-or-less normal distribution of 
    letters, as opposed to using 'Content here, 
    content here', making it look like readable English. 
    Many desktop publishing packages and web 
    page editors now use Lorem Ipsum as their 
    default model text, and a search for 'lorem ipsum' 
    will uncover many web sites still in their infancy. 
    Various versions have evolved over the years, 
    sometimes by accident, sometimes on 
    purpose (injected humour and the like).
    </p>

    【讨论】:

    • 我知道 :) 但我不知道使用什么 css 解决方案。我需要知道如何使用 php 函数来做到这一点
    • @CihanZengin PHP 是一个服务器端脚本,能够进行服务器级操作。前端展示由 CSS 处理。
    • 渲染完全在客户端在浏览器中完成,你无法控制使用 php。你所能做的就是传递样式/css
    【解决方案3】:

    您可以将段落存储为这样的字符串,并使用 RegExp 删除所有试用空白。

    $text = preg_replace('/\s+/', ' ', $text);

    【讨论】:

      猜你喜欢
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      • 2021-06-20
      相关资源
      最近更新 更多