Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Remove everything but allowed chars from a string

Hello

I've got a URL like this:
http://blahblah.com/index.php?show=XXXXX

I if there's something different than a-z, 0-9, _ or "-" in the variable "show", then it should be stripped.

some examples:
show=123 => show=123
show=../abc => show=abc
show=!?x+ => show=x
show=a-b_c => show=a-b_c


Is there a built-in php function that does something like that?

thanks,
saimen
0
saimenCH
Asked:
saimenCH
1 Solution
 
Marcus BointonCommented:
usual way to clean data like this is with a regex:

$var = preg_replace('/[^a-z0-9_-]/', '', $var);

That pattern says: replace anything in $var that's not a-z, 0-9 _ or - with nothing (i.e. delete it).

The - must be the first or last char in the match list so it doesn't get interpreted as a range delimiter (as in a-z).
0
 
saimenCHAuthor Commented:
Hi Squinky!

This was exactly the function I was looking for. It works perfect.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now