php 如何在php中的字符串中的每个字符后添加一个空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20246966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a space after every character in a string in php?
提问by Himanshu Chawla
I have a string in php named $password="1bsdf4";
我在 php 中有一个字符串,名为$password=" 1bsdf4";
I want output "1 b s d f 4"
我想要输出“1 bsdf 4”
How is it possible. I was trying implode function but i was not able to do..
这怎么可能。我正在尝试内爆功能,但我无法做到..
$password="1bsdf4";
$formatted = implode(' ',$password);
echo $formatted;
I tried this code:
我试过这个代码:
$str=array("Hello","User");
$formatted = implode(' ',$str);
echo $formatted;
Its working and adding space in hello and user ! Final Output I got Hello User
它的工作和在 hello 和 user 中添加空间!最终输出我得到了Hello User
Thanks, yours answers will be appreciated.. :)
谢谢,您的回答将不胜感激.. :)
回答by Pitchinnate
You can use implode you just need to use str_split first which converts the string to an array:
您可以使用内爆,您只需要首先使用 str_split 将字符串转换为数组:
$password="1bsdf4";
$formatted = implode(' ',str_split($password));
http://www.php.net/manual/en/function.str-split.php
http://www.php.net/manual/en/function.str-split.php
Sorry didn't see your comment @MarkBaker if you want to convert you comment to an answer I can remove this.
抱歉没有看到您的评论@MarkBaker,如果您想将您的评论转换为我可以删除的答案。
回答by kero
You can use chunk_splitfor this purpose.
您可以chunk_split为此目的使用。
$formatted = trim( chunk_split($password, 1, ' ') );
trimis necessary here to remove the whitespace after the last character.
trim这里有必要删除最后一个字符后的空格。
回答by Dejv
You can use this code [DEMO]:
您可以使用此代码[DEMO]:
<?php
$password="1bsdf4";
echo chunk_split($password, 1, ' ');
chunk_split()is build-in PHP function for splitting string into smaller chunks.
chunk_split()是内置的 PHP 函数,用于将字符串拆分为更小的块。
回答by Himanshu Chawla
This also Worked..
这也有效..
$password="1bsdf4";
echo $newtext = wordwrap($password, 1, "\n", true);
Output: "1 b s d f 4"
输出:“1 bsdf 4”
回答by Alexey Azbukin
function break_string($string, $group = 1, $delimeter = ' ', $reverse = true){
$string_length = strlen($string);
$new_string = [];
while($string_length > 0){
if($reverse) {
array_unshift($new_string, substr($string, $group*(-1)));
}else{
array_unshift($new_string, substr($string, $group));
}
$string = substr($string, 0, ($string_length - $group));
$string_length = $string_length - $group;
}
$result = '';
foreach($new_string as $substr){
$result.= $substr.$delimeter;
}
return trim($result, " ");
}
$password="1bsdf4";
$result1 = break_string($password);
echo $result1;
Output: 1 b s d f 4;
$result2 = break_string($password, 2);
echo $result2;
Output: 1b sd f4.
回答by Henk Jansen
How about this one
这个怎么样
$formatted = preg_replace("/(.)/i", "${1} ", $formatted);
according to: http://bytes.com/topic/php/answers/882781-add-whitespace-between-letters
根据:http: //bytes.com/topic/php/answers/882781-add-whitespace-between-letters

