Thoman
Thoman

Reputation: 792

Replace characters

I have a lot of strings of special characters. I want to replace all special characters with character "-" The characters do not need to be replaced as "-" are "A-Za-z0-9"

Upvotes: 1

Views: 354

Answers (2)

Jim
Jim

Reputation: 18853

Pretty straight forward.

$text = preg_replace('~[^0-9a-z]~i', '-', $text);

Extra Information

See Regular-Expressions.info for further information.

The ^ inside the Character Class [] at the start basically says, "Match any character except the ones following it".

Upvotes: 3

Colin Hebert
Colin Hebert

Reputation: 93177

try this :

preg_replace("/[^A-Za-z0-9-]/", "-", $yourString);

Resources :

Upvotes: 5

Related Questions