简体   繁体   English

PHP:在字符串中每4个字符替换3个字符

[英]PHP: replace 3 characters every 4th character in string

I have a string and I want to know how can I keep 1 character in a 4 character interval. 我有一个字符串,我想知道如何在4个字符的间隔中保留1个字符。

$string = "bc7yad3odf0x"; // and goes like this..

I want my new string to be: 我希望我的新字符串是:

yox

which comes out of bc7 y ad3 o df0 x 它来自 bc7 y ad3 o df0 x

The three characters to be ommited are always like char-char-number. 要省略的三个字符总是像char-char-number。

EDIT: Thank you all for answering. 编辑:谢谢大家的回答。 I tested all your answers and they work fine for me, except trisweb's preg_replace() solution, where I get "Unknown modifier 'g'". 我测试了你所有的答案,除了trisweb的preg_replace()解决方案之外,它们对我来说都很好,我得到了“Unknown modifier'g'”。 I accept Bhavik Shah's answer since I think is the simplest to my understanding. 我接受Bhavik Shah的回答,因为我觉得这对我的理解最简单。

只是它

preg_replace("/\w{3}(\w)/","$1",$string);
$nstr = "";
$str_array = array();

//split string in chunks of 4-char strings
$str_array = str_split($string, 4);

//loop through all elements of array and take the required character
foreach($str_array as $key => $value){
    $nstr .= $value[3];
}
echo $nstr;

More details about str_split . 有关str_split的更多详细信息。

$str = "bc7yad3odf0x";
$final_str = "";
$len = strlen($str);

for($i = 3; $i < $len; $i = $i+4) {
    $final_str .= substr($str, $i, 1)." ";
}

echo $final_str;
$newString = implode(
    '',
    array_map(
        function($value) {
            return substr($value,-1);
        },
        str_split($string,4)
    )
);
$string = "bc7yad3odf0x";
$new_string = "";
$iterations = ceil(strlen($string)/4);

for ($i=1;$i <= $iterations; $i++)
{
    $new_string .= substr($string, $i*4-1, 1);
}

print $new_string;

Seems like a job for a regular expression! 看起来像正常表达的工作!

$kept = preg_replace('/[a-z]{2}[0-9]/gi', "", $string);

What we're doing here is replacing occurrences of the kind "letter-letter-number" with a blank string, removing them. 我们在这里做的是用空白字符串替换“letter-letter-number”类型的出现,删除它们。

If the string is always of the form you say, then it should work fine. 如果字符串总是你说的形式,那么它应该工作正常。 However, if you always simply want the 4th character kept, you could use: 但是,如果您总是希望保留第4个字符,则可以使用:

$kept = preg_replace('/.{3}(.)/gi', "$1", $string);

Or, to use a similar idea of using backreferences to make the first form more strict, use: 或者,使用类似的使用反向引用的想法使第一个表单更严格,使用:

$kept = preg_replace('/[a-z]{2}[0-9](.)/gi', "$1", $string);

For more on using regular expressions in PHP, see this nice article. 有关在PHP中使用正则表达式的更多信息, 请参阅此文章。

in php you can access string like arrays 在PHP中,您可以访问类似数组的字符串

example: 例:

$text = "abcde";
echo $text[1]; // b

so you could take every forth character and concatenate it to your final string. 所以你可以把每个角色都拿出来并将它连接到你的最后一个字符串。 That is the easiest way i can think of at the moment. 这是我现在能想到的最简单的方法。

Better use regex: 更好地使用正则表达式:

$var = "bc7yad3odf0x";

$var = preg_replace("/[a-z]{2}[0-9]([a-z])/i","$1",$var);

echo $var;

$var = "bc7yad3odf0x";

$var = preg_replace("/[a-z]{2}[0-9]/i","",$var);

echo $var;

Try this 尝试这个

$y = 3;
for($i=0; $i<strlen($string); $i++){
    if($y < strlen($string)){
        $out .= substr($string,$y,1);
        $y = $y+4;
    }else{
        break;
    }
}
echo $out;

It's easy for-loop 这很容易循环

$ans = "";
for($i = 3, $l = strlen($s); $i < $l; $i += 4)
    $ans .= $s[$i];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM