user888750
user888750

Reputation:

Use regex to remove everything but [a-zA-Z0-9-] from string

I've search around on these forums, and it seems like everyone has a slight variation of my question.

I have the following string To-Wage-a-64-Bit-Coup! and I want to delete anything that's not a letter, number, or hyphen [a-zA-Z0-9] using preg_replace(). The end result would be that the exclamation mark would be removed.

I don't know all the characters which need to be removed, but I know what needs to stay. This is for generating a seo-friendly url. Not sure if a negative or a positive lookahead is what I need, or perhaps use something else like preg_match() to accomplish my ends.

Upvotes: 2

Views: 3120

Answers (2)

Tim Fountain
Tim Fountain

Reputation: 33148

$string = preg_replace("'[^A-Za-z0-9-]'", '', $string);

A-Za-z0-9- matches letters, numbers and the hyphen. The ^ at the start negates that character class, so instead it matches everything except those characters.

If this is for creating a URL friendly string you might also want to consider first converting spaces to hypens, removing multiple runs of hypens that may result from this, and converting the string to lower case.

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

preg_replace("([^a-zA-Z0-9-])","",$subject);

Put a ^ at the start of a [...] to negate its effect.

Upvotes: 8

Related Questions