0

假设我们有字符串$string="lolololol|lolololol|lolololol|lolololol"。因为我需要在浏览器中显示它,所以我想每 N(比如 50)个字符添加一个<wbr>标签(请参阅),但我不确定如何在 PHP 中执行此操作。我看到了一些使用正则表达式的其他语言的解决方案,但我实际上并不知道如何使用它,所以我更喜欢其他解决方案。

4

3 回答 3

4

您可以为此使用自动换行

<?php
$string="lolololol|lolololol|lolololol|lolololol";
echo wordwrap( $string, 50, "<wbr>", true);

演示

于 2012-10-12T18:58:25.673 回答
0
$string="lolololol|lolololol|lolololol|lolololol";
echo chunk_split($string, 7,"TRO");
于 2012-10-12T19:04:09.037 回答
0

红宝石 答案:


def put_wbr str, sub_str, num
  new_str = ""
  str.each_char{ |s| new_str.length % num == 0 ? new_str += sub_str : new_str += s }
  new_str[4..-1]
end

于 2013-08-21T12:29:23.060 回答