php str_replace 替换自身

发布于 2024-11-29 05:35:46 字数 467 浏览 8 评论 0原文

我需要替换出现的所有字母 aoie[aoieu]?
我尝试执行以下操作:

str_replace(array('a', 'o', 'i', 'e', 'u'), '[aoieu]?', $input);

但是当给它输入 black 而不是给我预期的 bl[aoieu]?ck 时,它给了我

bl[a[ao[aoi[aoie[aoieu]?]?[aoieu]?]?[aoie[aoieu]?]?[aoieu]?]?[aoi[aoie[aoieu]?]?[aoieu]?]?[aoie[aoieu]?]?[aoieu]?]?ck

如何让它不替换已经替换的东西?

I need to replace every occurrence of one of the letters a,o,i,e,u with [aoieu]?
I tried to do the following:

str_replace(array('a', 'o', 'i', 'e', 'u'), '[aoieu]?', $input);

But when giving it input of black instead of giving me the expected bl[aoieu]?ck it gave me

bl[a[ao[aoi[aoie[aoieu]?]?[aoieu]?]?[aoie[aoieu]?]?[aoieu]?]?[aoi[aoie[aoieu]?]?[aoieu]?]?[aoie[aoieu]?]?[aoieu]?]?ck

How can I get it to not replace things it already replaced?

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

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

发布评论

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

评论(7

尸血腥色 2024-12-06 05:35:46

您可以考虑为此使用正则表达式,或者您可以创建自己的函数,一次遍历字符串一个字母。这是一个正则表达式解决方案:

preg_replace('/[aoieu]/', '[aoieu]?', $input);

或者您自己的函数(请注意,$search只能是单个字符或字符数组,而不是字符串 - 您可以使用strpos或类似的构建一个也可以处理更长字符串的版本):

function safe_replace($search, $replace, $subject) {
  if(!is_array($search)) {
    $search = array($search);
  }
  $result = '';
  $len = strlen($subject);
  for($i = 0; $i < $len; $i++) {
    $c = $subject[$i];
    if(in_array($c, $search)) {
      $c = $replace;
    }
    $result .= $c;
  }
  return $result;
}
//Used like this:
safe_replace(array('a', 'o', 'i', 'e', 'u'), '[aoieu]?', 'black');

You can consider using a regular expression for this, or you can make your own function which steps through the string one letter at a time. Here's a regex solution:

preg_replace('/[aoieu]/', '[aoieu]?', $input);

Or your own function (note that $search only can be a single char or an array of chars, not strings - you can use strpos or similar to build one which handles longer strings as well):

function safe_replace($search, $replace, $subject) {
  if(!is_array($search)) {
    $search = array($search);
  }
  $result = '';
  $len = strlen($subject);
  for($i = 0; $i < $len; $i++) {
    $c = $subject[$i];
    if(in_array($c, $search)) {
      $c = $replace;
    }
    $result .= $c;
  }
  return $result;
}
//Used like this:
safe_replace(array('a', 'o', 'i', 'e', 'u'), '[aoieu]?', 'black');
白昼 2024-12-06 05:35:46

我建议避免 preglike 函数并使用 strtr()

此本机函数

  • 对输入字符串进行单次传递,
  • 不替换替换项,并
  • 查找要替换的最长匹配子字符串(当在另一个限定字符串中找到限定字符串时)

代码:

$result = strtr($input, array('a' => '[aoieu]?', 
                         'o' => '[aoieu]?', 
                         'i' => '[aoieu]?', 
                         'e' => '[aoieu]?', 
                         'u' => '[aoieu]?'));

I recommend avoiding preglike functions and using strtr().

This native function

  • makes a single pass over the input string,
  • does not replace replacements, and
  • finds the longest matching substring to replace (when a qualifying string is found within another qualifying string)

Code:

$result = strtr($input, array('a' => '[aoieu]?', 
                         'o' => '[aoieu]?', 
                         'i' => '[aoieu]?', 
                         'e' => '[aoieu]?', 
                         'u' => '[aoieu]?'));
最偏执的依靠 2024-12-06 05:35:46

你可能想尝试这个

<?php
$string = 'black';
$pattern = '/([aeiou])/i';
$replacement = '[aeiou]';
echo preg_replace($pattern, $replacement, $string);
?>

You might want to try this

<?php
$string = 'black';
$pattern = '/([aeiou])/i';
$replacement = '[aeiou]';
echo preg_replace($pattern, $replacement, $string);
?>
微暖i 2024-12-06 05:35:46

摘自文档:

更换订单陷阱

因为 str_replace() 从左到右替换,所以它可能会替换
进行多次替换时先前插入的值。

Taken from the documentation:

Replacement order gotcha

Because str_replace() replaces left to right, it might replace a
previously inserted value when doing multiple replacements.

永不分离 2024-12-06 05:35:46

您也许可以让 preg_replace 为您处理这个问题(请参阅 Thax、Emil 等的答案)。
否则,如果太复杂,您可以标记化:

$token = '{{{}}}';
// replace the temporary value with the final value
str_replace( $token, '[aoieu]?', 
    // replace all occurances of the string with a temporary value.
    str_replace( (array('a', 'o', 'i', 'e', 'u'), $token, $input ) );

You might be able to get preg_replace to handle this for you (see Thax, Emil, etc.'s answers).
Otherwise, if that is too complicated, you can, tokenize:

$token = '{{{}}}';
// replace the temporary value with the final value
str_replace( $token, '[aoieu]?', 
    // replace all occurances of the string with a temporary value.
    str_replace( (array('a', 'o', 'i', 'e', 'u'), $token, $input ) );
昔梦 2024-12-06 05:35:46
$input = str_replace(array('a', 'o', 'i', 'e', 'u'),   '~',          $input);
$input = str_replace('~',                              '[aoieu]?',   $input);
$input = str_replace(array('a', 'o', 'i', 'e', 'u'),   '~',          $input);
$input = str_replace('~',                              '[aoieu]?',   $input);
会发光的星星闪亮亮i 2024-12-06 05:35:46

这里是:

$output = preg_replace('/[aeiou]/', '[aeiou]?', $input);

Here it is:

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