Stooney Posted February 17, 2009 Share Posted February 17, 2009 I have the following two lines, each work fine without the other but not together. Example: Let's say i have 'File 1.jpg'. I want to make it File_1.jpg. I have this: $this->name=str_replace(' ', '_', $this->name); $this->name=ereg_replace("^[A-Za-z0-9_-]+$", '', $this->name).'.'.$this->getExt(); This gives me '.jpg'. I've tried commenting each line out, and each work as intended without the other. So line 1 will give me 'File_1.jpg' and line 2 will give me 'File 1.jpg' (no change). I'm assuming my pattern is wrong as far as the underscore goes. I built the expression based off what I read on the tutorial on the phpfreaks site. Also, how would I also allow parenthesis? I'd imagine it would something like: ^[A-Za-z0-9_-\\(\\)]+$ correct? Quote Link to comment https://forums.phpfreaks.com/topic/145499-expression-question/ Share on other sites More sharing options...
.josh Posted February 17, 2009 Share Posted February 17, 2009 what exactly are you wanting to do with the 2nd regex? Quote Link to comment https://forums.phpfreaks.com/topic/145499-expression-question/#findComment-763878 Share on other sites More sharing options...
Stooney Posted February 17, 2009 Author Share Posted February 17, 2009 Basically the end goal is to strip the string of all non-alphanumeric characters excluding the hyphen, underscore, and parenthesis. I also want to replace any whitespaces with an underscore. So.. S-tr*ing 1 would become S-tring_1 Quote Link to comment https://forums.phpfreaks.com/topic/145499-expression-question/#findComment-763886 Share on other sites More sharing options...
.josh Posted February 17, 2009 Share Posted February 17, 2009 Okay so after converting space to underscore, you want to strip out everything except for numbers, letters, hyphens, parenthesis, and underscores. I'm assuming by those concats in your OP that we're talking about the filename itself, and not the extension, as well? $this->name = preg_replace("~[^-\w\(\)]~", '', $this->name).'.'.$this->getExt(); Quote Link to comment https://forums.phpfreaks.com/topic/145499-expression-question/#findComment-763909 Share on other sites More sharing options...
Stooney Posted February 17, 2009 Author Share Posted February 17, 2009 That works great, ty CV And yea, the file extension is handled elsewhere. This is just for the file output. The uploaded file is renamed to a 10 digit random name, and a 'display name' is stored in the db. When the file is downloaded, it is sent under the display name, which I want only certain characters allowed in. Generally that doesn't make much sense, but it's an internal website for a business and they are technologically dumbtarded. In short, I rename the files like this to make my job easier when the files make it past the website stage of their life. Quote Link to comment https://forums.phpfreaks.com/topic/145499-expression-question/#findComment-763919 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.