Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Function to Remove several words from php string

Posted on 2011-09-28
2
Medium Priority
?
190 Views
Last Modified: 2012-06-21
Hi,

I have this:

$remove1 = "pdf"
$remove2="donwload"

$str="Download Adobe Dreamweaver training PDF"

Result should be:
$str="Adobe Dreamweaver training"

Can someone help me with a simple function to remove several word from string ?

It would be nice to call the function this way:
<?php echo $mystring("donwload","pdf") ?>

Thank you for any help!
0
Comment
Question by:Fernanditos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Expert Comment

by:Rik-Legger
ID: 36718011
function remove ($needles, $haystack) {
   return str_replace($needles, '', $haystack);
}


echo remove(array('download','pdf'), 'Download Adobe Dreamweaver training PDF');

Open in new window


Not tested, but something like this will work.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 36718927
http://www.laprbass.com/RAY_temp_fernanditos.php
<?php // RAY_temp_fernanditos.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA
$str="Download Adobe Dreamweaver training PDF";

// THE THINGS WE DO NOT WANT
$xxx = array
( "pdf"
, "download"
, "nonsense"
)
;

// A FUNCTION DEFINITION
function cleanup($str, $xxx)
{
    // CREATE A REGULAR EXPRESSION TO HANDLE CASE-INSENSITIVE REPLACEMENT
    $regex
    = '#'         // REGEX DELIMITER
    . '('         // START OF GROUP
    . implode('|', $xxx)
    . ')'         // END OF GROUP
    . '#'         // REGEX DELIMITER
    . 'i'         // CASE-INSENSITIVE
    ;

    return trim(preg_replace($regex, NULL, $str));
}
// SHOW THE WORK
echo cleanup($str, $xxx);

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
Suggested Courses

610 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