Jump to content

expression question


Stooney

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/145499-expression-question/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/145499-expression-question/#findComment-763886
Share on other sites

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();

 

Link to comment
https://forums.phpfreaks.com/topic/145499-expression-question/#findComment-763909
Share on other sites

That works great, ty CV :D

 

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. 

Link to comment
https://forums.phpfreaks.com/topic/145499-expression-question/#findComment-763919
Share on other sites

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.