替换 PHP 中除首次出现之外的所有特定字符

发布于 2024-08-19 08:19:22 字数 81 浏览 6 评论 0原文

示例

  • 输入 = 1.1.0.1
  • 预期输出 = 1.101

Example

  • Input = 1.1.0.1
  • Expected output = 1.101

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

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

发布评论

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

评论(7

倾`听者〃 2024-08-26 08:19:22

您可以使用 substr()str_replace() 相当容易:

$str = '1.1.0.1';
$pos = strpos($str,'.');
if ($pos !== false) {
    $str = substr($str,0,$pos+1) . str_replace('.','',substr($str,$pos+1));
}
echo $str;

You could make use substr() and str_replace() fairly easily:

$str = '1.1.0.1';
$pos = strpos($str,'.');
if ($pos !== false) {
    $str = substr($str,0,$pos+1) . str_replace('.','',substr($str,$pos+1));
}
echo $str;
孤云独去闲 2024-08-26 08:19:22
$s = preg_replace('/((?<=\.)[^.]*)\./', '$1', $s);

匹配零个或多个非点字符,后跟一个点,但前提是匹配前面有一个点。这可以防止初始数字的匹配。仅用在第 1 组中捕获的非点字符(数字)替换匹配项。

$s = preg_replace('/((?<=\.)[^.]*)\./', '$1', $s);

Matches zero or more non-dot characters followed by a dot, but only if the match was preceded by a dot. This prevents a match on the initial digit(s). Replaces the match with only the non-dot characters (the digits), which were captured in group #1.

叹梦 2024-08-26 08:19:22
$input="1.1.1.1";
$s = explode(".",$input ) ;
$t=array_slice($s, 1);
print implode(".",array($s[0] , implode("",$t)) );

或者

$input="1.1.1.1";
$s = explode(".",$input ,2) ;
$s[1]=str_replace(".","",$s[1]);
print implode(".",array($s[0] ,$s[1] ) );
$input="1.1.1.1";
$s = explode(".",$input ) ;
$t=array_slice($s, 1);
print implode(".",array($s[0] , implode("",$t)) );

or

$input="1.1.1.1";
$s = explode(".",$input ,2) ;
$s[1]=str_replace(".","",$s[1]);
print implode(".",array($s[0] ,$s[1] ) );
纵性 2024-08-26 08:19:22
  1. 匹配并释放第一个出现的文字点
  2. 替换所有后续文字点

代码:(演示)

echo preg_replace('~^[^.]*\.(*SKIP)(*FAIL)|\.~', '', $string);
// 1.101

或使用“继续”字符 (\G),消耗并忘记第一个文字点,然后替换所有后续文字点。

代码:(演示)

echo preg_replace('~(?:^[^.]*\.|\G(?!^))[^.]*\K\.~', '', $string);
// 1.101

或者简单地检查文字点是否在字符串中前面出现了文字点。

代码:(演示)

echo preg_replace('~(?<=\.)[^.]*\K\.~', '', $string);
// 1.101
  1. Match&Release the first occurring literal dot
  2. Replace all subsequent literal dots

Code: (Demo)

echo preg_replace('~^[^.]*\.(*SKIP)(*FAIL)|\.~', '', $string);
// 1.101

Or with the "continue" character (\G), consume and forget the first literal dot, then replace all subsequent literal dots.

Code: (Demo)

echo preg_replace('~(?:^[^.]*\.|\G(?!^))[^.]*\K\.~', '', $string);
// 1.101

Or simply check that a literal dot has a literal dot occurring earlier in the string.

Code: (Demo)

echo preg_replace('~(?<=\.)[^.]*\K\.~', '', $string);
// 1.101
两相知 2024-08-26 08:19:22

我虽然 substr_replace() 可以在这里工作,但遗憾的是没有......这是一个正则表达式方法:

$str = preg_replace('~(\d+\.)(\d+)\.(\d+)\.(\d+)~', '$1$2$3$4', $str);

I though substr_replace() would work here, but sadly no... Here is a regex approach:

$str = preg_replace('~(\d+\.)(\d+)\.(\d+)\.(\d+)~', '$1$2$3$4', $str);
℉絮湮 2024-08-26 08:19:22

您还可以使用 s 开关尝试以下正则表达式,

<?php
$string = '1.1.0.1';
$pattern = "/(?s)((?<=\.).*?)(\.)/i";
$replacement = "$1";
echo preg_replace($pattern, $replacement, $string);
?>

输出:

1.101

You could also try the below regex with s switch,

<?php
$string = '1.1.0.1';
$pattern = "/(?s)((?<=\.).*?)(\.)/i";
$replacement = "$1";
echo preg_replace($pattern, $replacement, $string);
?>

Output:

1.101
拧巴小姐 2024-08-26 08:19:22

使用正则表达式匹配可以更清晰地描述所需的结果,并避免调用 substrstrpos 的容易出错的方法。在这里,我假设第一个点之前不需要任何文本,即输入可能以必须保留的点开头。区别在于 *+ 量词是否适合以下模式。

如果您的输入始终很短,一个简单的方法是替换尾随点,直到没有剩余:

$count = 0;
$output = $input;
do {
    $output = preg_replace('/^(.*\.)(.+)\./', '$1$2', $output, -1, $count);
} while ($count != 0);
echo $output;

要使用单个正则表达式匹配来完成此操作,请使用 preg_replace_callback 应用函数 (str_replace 在这种情况下)到反向引用变量 $2

$output = preg_replace_callback(
  '/^([^.]*\.)(.+)$/',
  function ($m) { return $m[1] . str_replace('.', '', $m[2]); },
  $input);

示例结果:

1.1.0.1 - 1.101
.1.0.1  - .101
111     - 111
1.1     - 1.1
1.      - 1.
.1      - .1
.1.     - .1
....    - .

您可能需要试验

Using regex matches can be clearer by depicting the desired result and avoids the error-prone approach of calls to substr and strpos. Here I assume that no text is required before the first dot, i.e., that an input may begin with a dot that must be preserved. The difference is whether a quantifier of * or + is appropriate in the patterns below.

If your inputs will always be short, a straightforward approach is to replace trailing dots until none remain:

$count = 0;
$output = $input;
do {
    $output = preg_replace('/^(.*\.)(.+)\./', '$1$2', $output, -1, $count);
} while ($count != 0);
echo $output;

To do it with a single regex match, use preg_replace_callback to apply a function (str_replace in this case) to the backreference variable $2.

$output = preg_replace_callback(
  '/^([^.]*\.)(.+)$/',
  function ($m) { return $m[1] . str_replace('.', '', $m[2]); },
  $input);

Sample results:

1.1.0.1 - 1.101
.1.0.1  - .101
111     - 111
1.1     - 1.1
1.      - 1.
.1      - .1
.1.     - .1
....    - .

You may want to experiment with the code and test cases at Try It Online!

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