thevoipman
thevoipman

Reputation: 1833

remove whatever i want from string

I got a few keywords, symbols, letters etc I want to remove from my php string. I'm trying to add it but it doesn't work too well.

$string = preg_replace("/(?![=$'%-mp4mp3])\p{P}/u","", $check['title']);

pretty much I want to to remove word mp3, mp4, ./, apples from the string.

Please help guide me, thanks in advance!

Upvotes: 0

Views: 117

Answers (2)

madfriend
madfriend

Reputation: 2430

First: [] in regular expression introduces a character class. A hyphen is used to represent a character range between two symbols. So the reason your regular expression would make too many erasures (as I suppose) is because [=$'%-mp4mp3] means =, $, ', everything from % to m (72 characters actually!), p, 3, 4.

Second: your regular expression doesn't grab "bad" characters/keywords. Actually, you erase punctuation after bad characters/keywords, as negative lookahead is meta sequence (it is not included in match).

Change your regex to:

"/[=$'%-]|mp3|mp4/u"

Upvotes: 3

Nikola K.
Nikola K.

Reputation: 7155

You don't need regex for that.

$string = "Your original string here";
$keywords = array('mp3', 'mp4');
echo str_replace($keywords, '', $string);

Upvotes: 2

Related Questions