?
Solved

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

Posted on 2009-12-28
4
Medium Priority
?
498 Views
Last Modified: 2012-05-08
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
0
Comment
Question by:MathewT
  • 3
4 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 26135673
:)


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

0
 
LVL 83

Accepted Solution

by:
leakim971 earned 120 total points
ID: 26135676
too fast :(
<?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

0
 

Author Closing Comment

by:MathewT
ID: 31670651
you're good! thanks again!
0
 
LVL 83

Expert Comment

by:leakim971
ID: 26135719
Thank you again for the points!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question