views:

666

answers:

2

How can I convert

ereg_replace(".*\.(.*)$","\\1",$imgfile);

to

preg_replace... ?

?

I'm having trouble with it?

+1  A: 

delimiters, add any char to beginning and end of expression, in this case, and by tradition, the '/' character preg_replace('/.*\.(.*)$/',"\\1",$imgfile); The regex isn't very good, better to use strrpos and take substr().

Regex is slow, use this. $extension=substr($imgName,strrpos($imgName,'.'));

Jimmy Ruska
I know but having this problem;preg_replace() [function.preg-replace]: Unknown modifier '$' in C:\wamp\www..
sml
Yes, my bad, also the editing seems to destroy the expression. In the end better to use a substr() in this case. You could also use preg_match.
Jimmy Ruska
You can escape your code with backticks (see http://stackoverflow.com/editing-help). The $ fix looks good.
Matthew Flaschen
A: 
preg_replace("/.*\.(.*)$/", "\\1", "foo.jpg")

I don't know why PHP requires the / delimiters. The only reason Perl, JS, etc. have them is that they allow regex literals, which PHP doesn't.

Matthew Flaschen