Php 每N个字符将一个字符串插入另一个字符串
假设我们有字符串Php 每N个字符将一个字符串插入另一个字符串,php,string,Php,String,假设我们有字符串$string=“lolololol | lolololol | lolololol | lolololol”。因为我需要在浏览器中显示它,所以我想每N(比如说,50)个字符添加一个标记(),但我不确定如何在PHP中实现。我在其他语言中看到一些使用regex的解决方案,但我实际上不知道如何使用它,所以我更喜欢其他解决方案。你可以使用wordwrap <?php $string="lolololol|lolololol|lolololol|lolololol"; echo w
$string=“lolololol | lolololol | lolololol | lolololol”
。因为我需要在浏览器中显示它,所以我想每N(比如说,50)个字符添加一个
标记(),但我不确定如何在PHP中实现。我在其他语言中看到一些使用regex的解决方案,但我实际上不知道如何使用它,所以我更喜欢其他解决方案。你可以使用wordwrap
<?php
$string="lolololol|lolololol|lolololol|lolololol";
echo wordwrap( $string, 50, "<wbr>", true);
您可以使用wordwrap来实现这一点
<?php
$string="lolololol|lolololol|lolololol|lolololol";
echo wordwrap( $string, 50, "<wbr>", true);
Ruby回答:
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
鲁比回答:
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
所有那些lolo
都必须有一个tro
前缀…@MarcB,它只是一个占位符,没有空格文本作为示例,带有指向xD@Mr.Alien ? 一旦我知道了字符串的长度,我仍然需要把字符串放在哪里。所有那些lolo
只需要有一个tro
前缀…@MarcB,它只是一个占位符,没有空格文本作为例子,带有指向xD@Mr.Alien ? 一旦我知道了绳子的长度,我还需要把绳子放在哪里,知道放在哪里。这正好符合我的需要,谢谢。(只是在[PHP.net](PHP.net)上找不到该函数):|)完全符合我的需要,谢谢。(只是在[PHP.net](PHP.net):|上找不到函数)