user2263996
user2263996

Reputation: 47

Exclude symbol from regular expression PHP

I'm currently using the following to clean a string from symbols/unknown characters:

$title = preg_replace("/[^a-zA-Z0-9-]/", " ", $title);

However, I don't want to remove '&' from the string

Can someone help me out?

Thanks!

Upvotes: 2

Views: 8823

Answers (2)

hek2mgl
hek2mgl

Reputation: 158160

the ^ at the start of a character class: [^... ] means that all chars in that class should be excluded from matching. In your case this chars shouldn't be removed. So add & to the class like this:

$title = preg_replace("/[^a-zA-Z0-9-&;]/", " ", $title);

Upvotes: 2

Joban
Joban

Reputation: 1337

Take a look at this nice cheat sheet, it'll come in handy farther down the road.

enter image description here

Upvotes: 3

Related Questions