如何在 HTML 中的每个句号后面添加两个空格?

发布于 2024-12-08 17:29:29 字数 442 浏览 3 评论 0原文

我需要在我们整个网站的每个句子的每个句号后面有两个空格(不要问)。

一种方法是在每个句号后手动添加一个&nbsp 。这将需要几个小时。

我们不能只是查找并替换每个句点,因为我们在 PHP 和其他情况下有串联,先有一个句点,然后有一个空格,但它不在句子中。

有没有办法做到这一点...并且 Internet Explorer 6 中的一切仍然有效?

[编辑] - 棘手的部分是,在代码中,有一些 PHP 行包含周围有空格的点,如下所示:

<?php echo site_url('/css/' . $some_name .'.css');?>

我绝对不希望有额外的空格来打破这样的行,所以我很乐意添加两个可见的行所有 P 标记中的每个句点后有空格

I need there to be two spaces after every period in every sentence in our entire site (don't ask).

One way to do it is to embark on manually adding a    after every single period. This will take several hours.

We can't just find and replace every period, because we have concatenations in PHP and other cases where there is a period and then a space, but it's not in a sentence.

Is there a way to do this...and everything still work in Internet Explorer 6?

[edit] - The tricky part is that in the code, there are lines of PHP that include dots with spaces around them like this:

<?php echo site_url('/css/' . $some_name .'.css');?>

I definitely don't want extra spaces to break lines like that, so I would be happy adding two visible spaces after each period in all P tags.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

桃扇骨 2024-12-15 17:29:29

众所周知,HTML 会折叠空白,但它只是为了显示而这样做。多余的空间仍然存在。因此,如果源材料在每个句点后创建了两个空格,那么所建议的一些替换方法可以可靠地工作 - 搜索“句点-空间-空间”并将其替换为更合适的内容,例如句点-空间- 。请注意,您不应使用  因为它会妨碍边缘的正确换行。 (如果您使用 Ragged Right,只要您在空格之前使用 nbsp,边距变化就不会明显。)

您还可以将每个句子包裹在一个跨度中,并使用 :after 选择器添加空格并将其格式化为“字间距”宽。或者,您可以直接将句子本身之间的空间包裹在跨度和样式中。

我为博主编写了一个 JavaScript 解决方案,它可以即时执行此操作,查找句点-空间-空间,并将其替换为看起来更宽的跨度样式空间。

然而,如果您的原始材料不包含此类内容,那么您将不得不研究句子边界检测算法(这并不那么简单),然后修改一个算法以使其不会绊倒 PHP 代码。

As we all know, HTML collapses white space, but it only does this for display. The extra spaces are still there. So if the source material was created with two spaces after each period, then some of these substitution methods that are being suggested can be made to work reliably - search for "period-space-space" and replace it with something more suituble, like period-space- . Please note that you shouldn't use   because it can prevent proper wrapping at margins. (If you're using ragged right, the margin change won't be noticeable as long as you use the the nbsp BEFORE the space.)

You can also wrap each sentence in a span and use the :after selector to add a space and format it to be wide with "word-spacing". Or you can wrap the space between sentences itself in a span and style that directly.

I've written a javascript solution for blogger that does this on the fly, looks for period-space-space, and replaces it with a spanned, styled space that appears wider.

If however your original material doesn't include this sort of thing then you'll have to study up on sentence boundary detection algorithms (which are not so simple), and then modify one to also not trip over PHP code.

归途 2024-12-15 17:29:29

您也许可以使用 JavaScript split 方法或正则表达式,具体取决于文本的范围。

这是分割方法:

var el = document.getElementById("mydiv");
if (el){
    el.innerText = el.innerText.split(".").join(".\xA0 ");   
}

测试用例:

Hello world。在句号后面插入空格。使用 split 方法。

结果:

世界你好。在句号后插入空格。使用分割方法。

You might be able to use the JavaScript split method or regex depending on the scope of the text.

Here's the split method:

var el = document.getElementById("mydiv");
if (el){
    el.innerText = el.innerText.split(".").join(".\xA0 ");   
}

Test case:

Hello world.Insert spaces after the period.Using the split method.

Result:

Hello world. Insert spaces after the period. Using the split method.

冷情 2024-12-15 17:29:29

您是否考虑过使用输出缓冲区? ob_start($callback)

未测试,但如果您愿意将其粘贴在任何输出之前(或者更好的是,卸载该函数):

<?php
    function processDots($buffer)
    {
      return (str_replace(".", ". ", $buffer));
    }

    ob_start("processDots");
?>

并将其添加到输入末尾:

<?php ob_end_flush(); ?>

可能会起作用:)

Have you thought using output buffer? ob_start($callback)

Not tested, but if you'll stick this before any output (or betetr yet, offload the function):

<?php
    function processDots($buffer)
    {
      return (str_replace(".", ". ", $buffer));
    }

    ob_start("processDots");
?>

and add this to end of input:

<?php ob_end_flush(); ?>

Might just work :)

飞烟轻若梦 2024-12-15 17:29:29

如果您不反对“后处理”/“javascript”解决方案:

var nodes = $('*').contents().map(function(a, b) {
    return (b.nodeType === Node.TEXT_NODE ? b : null);
});
$.each(nodes, function(i,node){
    node.data = node.data.replace(/(\.\s)/g, '.\u00A0\u00A0');
});

为了简洁而使用 jQuery,但不是必需的。

ps 我看到您关于并非所有句点和空格都适用的评论得到平等对待,但这已经是最好的了。否则,您将需要更好/更防弹的方法。

If you're not opposed to a "post processing"/"javascript" solution:

var nodes = $('*').contents().map(function(a, b) {
    return (b.nodeType === Node.TEXT_NODE ? b : null);
});
$.each(nodes, function(i,node){
    node.data = node.data.replace(/(\.\s)/g, '.\u00A0\u00A0');
});

Using jQuery for the sake of brevity, but not required.

p.s. I saw your comment about not all periods and a space are to be treated equal, but this is about as good as it gets. otherwise, you're going to need a lot better/more bullet-proof approach.

日久见人心 2024-12-15 17:29:29

将这样的内容合并到您的 PHP 文件中:

<?php if (preg_match('/^. [A-Z]$/' || '/^.  [A-Z]$/')) { preg_replace('. ', '.  '); } ?>

这允许您搜索每个新句子的开头,如 .spacespaceA-Z.spaceA-Z 中,然后替换与 . space 一起使用。 [注:大写字母不被替换]

Incorporate something like this into your PHP file:

<?php if (preg_match('/^. [A-Z]$/' || '/^.  [A-Z]$/')) { preg_replace('. ', '.  '); } ?>

This allows you to search for the beginning of each new sentence as in .spacespaceA-Z, or .spaceA-Z and then replaces that with . space. [note: Capital letter is not replaced]

┊风居住的梦幻卍 2024-12-15 17:29:29

elbrant 的代码不起作用。我复制了整个内容,但到处都出现验证错误。

我尝试将其添加到 WordPress 中的functions.php 中,而无需开始和结束括号。 GeneratePress 也破坏了它。

错误 解析错误:语法错误,意外标记“<”,第 5 行代码中期望出现文件结尾

The code from elbrant doesn't work. I copied the entire thing but get validation errors everywhere.

I tried adding it to functions.php in WordPress without the beginning and ending brackets. GeneratePress also breaks it.

error Parse error: syntax error, unexpected token "<", expecting end of file in your code on line 5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文