?
Solved

Function to Remove several words from php string

Posted on 2011-09-28
2
Medium Priority
?
188 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

765 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