【问题标题】:Find characters in string that are present in an array and remove them - php查找字符串中存在于数组中的字符并删除它们 - php
【发布时间】:2014-01-15 19:44:03
【问题描述】:

假设我有以下字符串:$test ='abcd.gdda<fsa>dr';

还有以下数组:$array = array('<','.');

如何找到 $array 数组中元素的字符位置? (可能的最快方式)然后存储和删除它们(不在该特定索引处留下 NULL 值)?

附言。我知道我可以使用 strpos() 来检查每个元素,但这会输出如下内容: 9, 5 因为在 '.' 之前搜索了 '

【问题讨论】:

  • 而不是strpos() 使用strrpos(),开始寻找从右到左的字符串。所以当你替换< 时,字符. 仍然会在同一个位置。
  • 如果我使用这种方法...我将如何检查所有事件?这总是会在找到第一个元素后停止...
  • 您还需要分别存储每个搜索字符的位置吗?还是只是找到这些字符的位置?
  • @Cthulhu 是的,我愿意......而且也是按升序排列

标签: php arrays string


【解决方案1】:

适用于字符串和字符:

<?php
    $test ='abcda.gdda<fsa>dr';
    $array = array('<', '.', 'dr', 'a'); // not also characters but strings can be used
    $pattern = array_map("preg_quote", $array);
    $pattern = implode("|", $pattern);
    preg_match_all("/({$pattern})/", $test, $matches, PREG_OFFSET_CAPTURE);
    array_walk($matches[0], function(&$match) use (&$test)
    {
        $match = $match[1];
    });
    $test = str_replace($array, "", $test);
    print_r($matches[0]); // positions
    echo $test;

输出

Array
(
    [0] => 0
    [1] => 4
    [2] => 5
    [3] => 9
    [4] => 10
    [5] => 13
    [6] => 15
)
bcdgddfs>

【讨论】:

  • 此示例仅适用于长度为 1char 的字符 example。你能解决这个问题吗?
  • @Glavić 但是 OP 需要找到字符位置而不是字符串,但是我会的。
  • 我在考虑会有同样问题的用户,所以为什么不支持多字符位置呢。
【解决方案2】:

查找所有位置,将它们存储在数组中:

$test = 'abcd.gdda<fsa>dr<second . 111';
$array = array('<','.');
$positions = array();

foreach ($array as $char) {
    $pos = 0;
    while ($pos = strpos($test, $char, $pos)) {
        $positions[$char][] = $pos;
        $pos += strlen($char);
    }
}

print_r($positions);
echo str_replace($array, '', $test);

demo

【讨论】:

  • 它看起来不错......但是..我如何组合结果以便我只有一个数组,其中元素按升序排列而不是二维数组?
  • @SpiderLinked:你为什么需要那个?您可以获得&lt; 位置的数组,例如$positions['&lt;']。如果您仍然希望将所有位置放在一个数组中,那么您将必须制作一个不能重复的键,例如 char&lt;position&gt; => position 或类似的东西。
  • 我需要它,因为我需要轻松地使用这些位置...我的数组由 47 个元素组成,我不能使用 $position[$char][]...所有东西.. .plus ..我需要按升序排列所有位置..
  • @SpiderLinked:就像我说的,那么你必须创建一些不能重复的特殊键,比如:demo。如果您不需要知道 char 在什么位置,那就更简单了:demo.
  • 我不能只内爆数组然后使用 str_split 吗?
【解决方案3】:

这是一种可能的解决方案,取决于每个搜索元素都是单个字符。

$array = array(',', '.', '<');
$string = 'fdsg.gsdfh<dsf<g,gsd';

$search = implode($array); $last = 0; $out = ''; $pos = array();
foreach ($array as $char) $pos[$char] = array();

while (($len = strcspn($string, $search)) !== strlen($string)) {
    $last = ($pos[$string[$len]][] = $last + $len) + 1;
    $out .= substr($string, 0, $len);
    $string = substr($string, $len+1);
}
$out.=$string;

演示:http://codepad.org/WAtDGr7p

【讨论】:

    【解决方案4】:

    编辑:
    PHP 有一个内置函数

    str_replace($array, "", $test);
    

    原始答案:
    这是我的做法:

    <?php
    $test ='abcd.gdda<fsa>dr';
    $array = array('<','.');
    foreach($array as $delimiter) {
      // For every delimiter explode the text into array and the recombine it
      $exploded_array = explode($delimiter, $test);
      $test = implode("", $exploded_array);
    }
    ?>
    

    有更快的方法来做到这一点(你会获得一些微秒)但是如果你想要速度,为什么要使用 php :) 我更喜欢简单。

    【讨论】:

      【解决方案5】:
      strtr($str, ['<' => '', '.' => '']);
      

      这可能会优于其他任何东西,因为它不需要您在 PHP 中迭代任何东西。

      【讨论】:

      • strtr 用于查找字符串而不是替换它
      猜你喜欢
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2019-07-23
      • 2019-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多