开发者

Strip punctuation in an address field in PHP

开发者 https://www.devze.com 2023-03-02 21:16 出处:网络
Hey all.I\'m having some trouble getting punctuation to be stripped out of an address field... Basically I want to take things like:

Hey all. I'm having some trouble getting punctuation to be stripped out of an address field...

Basically I want to take things like:

1234 Apple St. N.

And turn it into:

1234 Apple St N

A period is really the only piece of punctuation I can envision... but I suppose I'd really want to strip EVERYTHING o开发者_开发问答ut. Can somebody help me here? Nothing i do works... argh!


What's wrong with php's str_replace ? This will replace all occurences of a specified string with a replacement string (including a zero length string "").


You can use a preg_replace get the desired result. and \w is short-hand for [a-zA-Z0-9_], FYI.

$newAddress = preg_replace('/[^\w\s]/','',$oldAddress);

EDIT Now that I think about it, you probably want [^\w\s] so you don't remove spaces as well.

DEMO

0

精彩评论

暂无评论...
验证码 换一张
取 消