August 21, 200818 yr I am trying to use preg_replace to replace all percent encodes (%20, etc) with underscores. I cant figure out the pattern syntax though, it confuses me to no end. Basically, I need it to find all % chars and strip that char, plus the next two char, and replace those three char with an underscore. Anyone got some experience using this pattern system?
August 22, 200818 yr This regular expression will do it: %[0-9]{2} The % captures the literal percent sign. The [0-9] captures numbers from 0 to 9, and the {2} specifies there must be exactly two of the numbers. $niceUrl = preg_replace('%[0-9]{2}', '_', $oldUrl); Some helpful regex links: http://www.regular-expressions.info/tutorial.html http://www.addedbytes.com/cheat-sheets/reg...ns-cheat-sheet/
August 22, 200818 yr Author have you tried using urldecode($theURLyouwishtouse) No, in fact I am using rawURLencode to turn them into these symbols because then I can search by a pattern to find them and delete them. Or I would if I could get the syntax right. EDIT: yeah, a good reply! Thank you so much for clarifing. One thing though, the two digits are in hex so 0-9 wont catch the letters... can I use [A-Fa-f0-9] to get it? Edit: I got it working beautifully and I'm cleaning up my code now. Thank you for the help.
August 22, 200818 yr Author Here is the finished product. move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.str_replace(" ", "_", trim(preg_replace('"[^0-9a-z]"'," ",strtolower(substr($_FILES['file']['name'], 0, strrpos($_FILES['file']['name'], ".")))))).strstr($_FILES['file']['name'], ".")); It will completely sterilize a file name down to alpha-numeric and underscores for all interior spaces. Any symbols in the name are turned into underscores and no spaces or underscores are left before or after the name. it even removes spaces and symbols from between the extension and the name.
Create an account or sign in to comment