Solved

Function to Remove several words from php string

Posted on 2011-09-28
2
186 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
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 110

Accepted Solution

by:
Ray Paseur earned 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 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