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

标签 php string replace intervals

我有一个字符串,我想知道如何在 4 个字符的间隔中保留 1 个字符。

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

我希望我的新字符串是:

yox

来自 bc7 y ad3 o df0 x

要省略的三个字符始终类似于 char-char-number。

编辑:谢谢大家的回答。我测试了你所有的答案,它们对我来说工作得很好,除了 trisweb 的 preg_replace() 解决方案,我得到“未知修饰符‘g’”。 我接受 Bhavik Shah 的回答,因为我认为这是我理解的最简单的回答。

最佳答案

就这样

preg_replace("/\w{3}(\w)/","$1",$string);

关于PHP:字符串中每 4 个字符替换 3 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14813934/

相关文章:

python - pandas.replace 与 str.replace 正则表达式冲突。代码顺序

MySql 删除所有与表值匹配的子字符串

javascript - 如何将 HTML 重写为 PHP 回显?

php - Tinymce 使用 php 获取内容

c - 如何用等效的 strcat() 替换 memcpy() ?

excel - VBA - 根据条件更改单元格的值

php - str_replace : Match whole word only

php - 删除 Laravel 4 中多对多模型的关系

php - PHP 中的正则表达式 : how to match any A and IMG HTML tag including content

python - 如何在python中使用pandas执行字符串替换操作?