简体   繁体   中英

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!

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

在此处输入图片说明

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM