Solved

Function to Remove several words from php string

Posted on 2011-09-28
2
181 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 108

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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 is …
The viewer will learn how to dynamically set the form action using jQuery.
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now