Link to home
Start Free TrialLog in
Avatar of MathewT
MathewT

asked on

PHP - Remove a substring from a string if it exists in an array

if I have an array with values for example

cat
dog
fish
horse

And I want to remove these values from a long string that may or may not contain 1 or more of these array strings, how can I get the return value with these array values (if they exist in the string).

so passing the string "monkeyfishtigerdog" would return "monkeytiger".

Thanks,

Mathew
Avatar of leakim971
leakim971
Flag of Guadeloupe image

:)


<?php
function F_remove_substring_exist($A_array,$V_string){
 return preg_replace("/" . implode("|", $A_array) ."/i", $V_string)
}

$A_array = array('my', 'string'); 
F_remove_substring_exist($A_array,'abmsc');
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MathewT
MathewT

ASKER

you're good! thanks again!
Thank you again for the points!