SpiderLinked
SpiderLinked

Reputation: 373

Find characters in string that are present in an array and remove them - php

Let's say i have the following string: $test ='abcd.gdda<fsa>dr';

And the following array: $array = array('<','.');

How can i find the positions of the characters that are elements in the $array array? (fastest way possible) and then store and remove them (without leaving NULL values at that specific index)?

Ps. I know that i could use strpos() to check for each element but that would output something like: 9, 5 because the '<' symbol is searched for before the '.' element and that causes the function to believe that the '<' is before '.' in the string. I tried combining this with the sort() function...but i does not work as expected... (it outputs some NULL positions...)

Upvotes: 3

Views: 6822

Answers (5)

Rahul Prasad
Rahul Prasad

Reputation: 8222

EDIT:
PHP has an inbuild function for that

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

ORIGINAL ANSWER:
Here is how I would do it:

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

There are faster ways to do it (you will gain some microseconds) but why would you use php if you wanted speed :) I mostly prefer simplicity.

Upvotes: 0

revo
revo

Reputation: 48711

Works with both strings and characters:

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

Output:

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

Upvotes: 3

Aleksei Averchenko
Aleksei Averchenko

Reputation: 1776

strtr($str, ['<' => '', '.' => '']);

This will probably outperform anything else, because it doesn't require you to iterate over anything in PHP.

Upvotes: -1

Glavić
Glavić

Reputation: 43552

Find all positions, store them in array:

$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

Upvotes: 1

Cthulhu
Cthulhu

Reputation: 1372

Here is one possible solutions, depending on every searching element being a single character.

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

Demo: http://codepad.org/WAtDGr7p

Upvotes: 0

Related Questions