Jump to content

Limiting a string to certain characters


tibberous

Recommended Posts

I am trying to strip out everything from a string except letters, numbers, periods and forward slashes. I figure this will help prevent me a little bit from attacks.

 

Now, this works: $uri = preg_replace("/[^A-Za-z0-9\-\.\/]/", '', $uri);

 

But it that going to be really bad performance wise?

Link to comment
Share on other sites

performance is measured in comparison to something...

to what you would like to compare this too? I don't know..

Run some performance tests in a loop with str_replace.. or whatever it is you'd like to compare it too and find out.

 

Generally speaking though, it depends on the amount of data you are running the regex through, even still, preg is fast.

 

BTW: Your regex will also strip the spaces from a string.

Try:

/[^A-Za-z0-9\-\.\/ ]/

<a href="http://nancywalshee03.freehostia.com/regextester/regex_tester.php?seeSaved=ukbjrqpe">Tested here.</a>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.