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
https://forums.phpfreaks.com/topic/82888-limiting-a-string-to-certain-characters/
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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.