Php Strlen将每[x]个字符剥离一次

Php Strlen将每[x]个字符剥离一次,php,strlen,Php,Strlen,我试着脱掉下面的每三个角色(在例子中是句号),这是我最好的猜测,和我得到的差不多,但我遗漏了一些东西,可能是次要的。这种方法(如果我能让它工作的话)会比正则表达式匹配更好吗 $arr = 'Ha.pp.yB.ir.th.da.y'; $strip = ''; for ($i = 1; $i < strlen($arr); $i += 2) { $arr[$i] = $strip; } $arr='Ha.pp.yB.ir.th.da.y'; $strip=''; 对于($i=1;$i

我试着脱掉下面的每三个角色(在例子中是句号),这是我最好的猜测,和我得到的差不多,但我遗漏了一些东西,可能是次要的。这种方法(如果我能让它工作的话)会比正则表达式匹配更好吗

$arr = 'Ha.pp.yB.ir.th.da.y';
$strip = '';
for ($i = 1; $i < strlen($arr); $i += 2) {
$arr[$i] = $strip; 
}
$arr='Ha.pp.yB.ir.th.da.y';
$strip='';
对于($i=1;$i
一种方法是:

<?php
$oldString = 'Ha.pp.yB.ir.th.da.y';
$newString = "";

for ($i = 0; $i < strlen($oldString ); $i++) // loop the length of the string
{
  if (($i+1) % 3 != 0) // skip every third letter
  {
    $newString .= $oldString[$i];  // build up the new string
  }
}
// $newString is HappyBirthday
echo $newString;
?>


或者,如果要删除的字母始终相同,explode()函数可能会起作用。

一种方法是:

<?php
$oldString = 'Ha.pp.yB.ir.th.da.y';
$newString = "";

for ($i = 0; $i < strlen($oldString ); $i++) // loop the length of the string
{
  if (($i+1) % 3 != 0) // skip every third letter
  {
    $newString .= $oldString[$i];  // build up the new string
  }
}
// $newString is HappyBirthday
echo $newString;
?>

或者,如果要删除的字母始终是同一个字母,explode()函数可能会起作用。

这可能会起作用:

echo preg_replace('/(..)./', '$1', 'Ha.pp.yB.ir.th.da.y');
为了使其具有通用性:

echo preg_replace('/(.{2})./', '$1', $str);
此处的
2
表示保留两个字符,然后丢弃下一个。

这可能会起作用:

echo preg_replace('/(..)./', '$1', 'Ha.pp.yB.ir.th.da.y');
为了使其具有通用性:

echo preg_replace('/(.{2})./', '$1', $str);
此处的
2
表示保留两个字符,然后丢弃下一个。

一种方法:

$old = 'Ha.pp.yB.ir.th.da.y';
$arr = str_split($old); #break string into an array

#iterate over the array, but only do it over the characters which are a
#multiple of three (remember that arrays start with 0)
for ($i = 2; $i < count($arr); $i+=2) {
    #remove current array item
    array_splice($arr, $i, 1);
}
$new = implode($arr); #join it back
一种方法:

$old = 'Ha.pp.yB.ir.th.da.y';
$arr = str_split($old); #break string into an array

#iterate over the array, but only do it over the characters which are a
#multiple of three (remember that arrays start with 0)
for ($i = 2; $i < count($arr); $i+=2) {
    #remove current array item
    array_splice($arr, $i, 1);
}
$new = implode($arr); #join it back
我只需要使用和一个回调函数。大致看起来是这样的:

function remove_third_char( $text ) {
    return substr( $text, 0, 2 );
}

$text = 'Ha.pp.yB.ir.th.da.y';
$new_text = str_split( $text, 3 );

$new_text = array_map( "remove_third_char", $new_text );

// do whatever you want with new array
我只需要使用和一个回调函数。大致看起来是这样的:

function remove_third_char( $text ) {
    return substr( $text, 0, 2 );
}

$text = 'Ha.pp.yB.ir.th.da.y';
$new_text = str_split( $text, 3 );

$new_text = array_map( "remove_third_char", $new_text );

// do whatever you want with new array

谢谢你的帮助,特别是评论,但是我得到的结果是a.p.B.r.h.a?看起来我的测试有点不对劲,我现在已经修好了,谢谢你的帮助,尤其是评论,但是我得到的结果是a.p.B.r.h.a?看起来我的测试有点不对劲,我现在已经修好了,你可以在preg_replace('/([a-Za-z]{2})。'/','$1',$str);将更安全地替换(“/([A-Za-z]{2})。/”、“$1”、“$str);会更安全