preg_replace help for a single quote

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

preg_replace help for a single quote

Post by tecktalkcm0391 »

How can I make this preg_replace be only letters and an single quote ( ' )

Code: Select all

$cat = preg_replace('/[^a-z]/i', '',url_decode($_GET['cat']));
Thanks!
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

I assume you meant anything other than letters and single quotes (because of the ^ you have in your code).

Code: Select all

$regex = "/[^a-z']/i";
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

No, I wanted only letters and a single quote. I'm not good a this preg_replace and match stuff and I got it on a web site.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Well, ^ inside [] means "anything but what's inside the []".
If what you want it only letters and single quotes then try this:

Code: Select all

$regex = "/^[a-z']+$/iD";
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Oren wrote:Well, ^ inside [] means "anything but what's inside the []".
If what you want it only letters and single quotes then try this:

Code: Select all

$regex = "/^[a-z']+$/iD";
Thanks it worked!

Now thats lead me to another problem I didn't see coming. I have a mod_rewrite script of:

Code: Select all

 RewriteRule browse/([A-Za-z']+)$ /browser.php?cat=$1 [NC]
 RewriteRule browse/([A-Za-z']+)/([0-9]+)$ /browser.php?cat=$1&p=$2 [NC]
Which allows the letters and single quote, but when the URL comes of for /browse/Chris' Items it comes up /browse/Chris'%20Items.

How can I fix that to allow the %20
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Well, you will need to allow digits and %'s too:

Code: Select all

$regex = "/^[a-z0-9%']+$/iD";
Post Reply