3

i have the following array

(
[0] => DHL - 4857998880
[1] => DHL - 4858005666
[2] => COA - 485344322
)

i want to loop through the array and if DHL found then i want to remove from the array. the numbers in front of DHL do not matter. any element with DHL in front i want to remove from the array.

i have created the following the regular expression to ignore the numbers in front but not sure how move forward from there.

foreach($result as $valDHL) {

   $s = preg_replace("/[^a-z-]/i", "", $valDHL);

}
0

1 Answer 1

1

You can use array_filter to strip out the entries in your array that start with DHL, using the regex ^DHL to see if the entry starts with DHL:

$array = array(
0 => 'DHL - 4857998880',
1 => 'DHL - 4858005666',
2 => 'COA - 485344322'
);
$array = array_filter($array, function ($v) { return !preg_match('/^DHL/', $v); });
print_r($array);

Output:

Array (
  [2] => COA - 485344322 
)

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.