Strlen 删除每 [x] 个字符

发布于 2024-11-03 17:09:51 字数 236 浏览 3 评论 0原文

我正在尝试删除下面的每三个字符(在示例中为句点),这是我最好的猜测,并且与我得到的接近,但我错过了一些东西,可能很小。另外,这个方法(如果我能让它工作)会比正则表达式匹配、删除更好吗?

$arr = 'Ha.pp.yB.ir.th.da.y';
$strip = '';
for ($i = 1; $i < strlen($arr); $i += 2) {
$arr[$i] = $strip; 
}

I'm trying strip every third character (in the example a period) below is my best guess and is close as ive gotten but im missing something, probably minor. Also would this method (if i could get it working) be better than a regex match, remove?

$arr = 'Ha.pp.yB.ir.th.da.y';
$strip = '';
for ($i = 1; $i < strlen($arr); $i += 2) {
$arr[$i] = $strip; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白芷 2024-11-10 17:09:51

您可以做到这一点的一种方法是:

<?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()函数可能会起作用。

One way you can do it is:

<?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;
?>

Alternatively the explode() function might work, if the letter you're trying to remove is always the same one.

神也荒唐 2024-11-10 17:09:51

这可能有效:

echo preg_replace('/(..)./', '$1', 'Ha.pp.yB.ir.th.da.y');

为了使其通用:

echo preg_replace('/(.{2})./', '$1', $str);

在此上下文中,2 表示您保留两个字符,然后丢弃下一个字符。

This might work:

echo preg_replace('/(..)./', '$1', 'Ha.pp.yB.ir.th.da.y');

To make it general purpose:

echo preg_replace('/(.{2})./', '$1', $str);

where 2 in this context means you are keeping two characters, then discarding the next.

草莓味的萝莉 2024-11-10 17:09:51

一种方法:

$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';
$new = preg_replace('/(..)\./', '$1', $old);
#selects any two characters followed by a dot character
#alternatively, if you know that the two characters are letters,
#change the regular expression to:
/(\w{2})\./

A way of doing it:

$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

Or, with a regular expression:

$old = 'Ha.pp.yB.ir.th.da.y';
$new = preg_replace('/(..)\./', '$1', $old);
#selects any two characters followed by a dot character
#alternatively, if you know that the two characters are letters,
#change the regular expression to:
/(\w{2})\./
不疑不惑不回忆 2024-11-10 17:09:51

我只使用 array_map 和回调函数。它看起来大致是这样的:

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

I'd just use array_map and a callback function. It'd look roughly like this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文