ereg_replace to preg_replace?
Asked Answered
L

3

12

How can I convert

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

to

preg_replace... ?

?

I'm having trouble with it?

Lemuel answered 14/3, 2010 at 21:44 Comment(0)
D
11
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.

Devon answered 14/3, 2010 at 21:49 Comment(2)
It is because they opt to use the single string to provide both the modifier and the regex pattern. "/[a-z]/i".Lyell
See php.net/manual/en/regexp.reference.delimiters.php .. for example, $a = 'string/with/slashes'; $b = preg_replace('#/with/#', '-without-', $a); is valid, sets $b to string-without-slashes and you do not have to escape the / in the regex given to preg_replace.Jahn
L
23

You should know 4 main things to port ereg patterns to preg:

  1. Add delimiters(/): 'pattern' => '/pattern/'

  2. Escape delimiter if it is a part of the pattern: 'patt/ern' => '/patt\/ern/'
    Achieve it programmatically in following way:
    $ereg_pattern = '<div>.+</div>';
    $preg_pattern = '/' .addcslashes($ereg_pattern, '/') . '/';

  3. eregi(case-insensitive matching): 'pattern' => '/pattern/i' So, if you are using eregi function for case insenstive matching, just add 'i' in the end of new pattern('/pattern/').

  4. ASCII values: In ereg, if you use number in the pattern, it is assumed that you are referring to the ASCII of a character. But in preg, number is not treated as ASCII value. So, if your pattern contain ASCII value in the ereg expression(for example: new line, tabs etc) then convert it to hexadecimal and prefix it with \x.
    Example: 9(tab) becomes \x9 or alternatively use \t.

Hope this will help.

Linebreeding answered 10/6, 2013 at 22:26 Comment(1)
There is a built in for escaping a regex string between delimiters php.net/manual/en/function.preg-quote.phpHandcraft
D
11
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.

Devon answered 14/3, 2010 at 21:49 Comment(2)
It is because they opt to use the single string to provide both the modifier and the regex pattern. "/[a-z]/i".Lyell
See php.net/manual/en/regexp.reference.delimiters.php .. for example, $a = 'string/with/slashes'; $b = preg_replace('#/with/#', '-without-', $a); is valid, sets $b to string-without-slashes and you do not have to escape the / in the regex given to preg_replace.Jahn
C
1

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,'.'));

Christan answered 14/3, 2010 at 21:47 Comment(3)
I know but having this problem; preg_replace() [function.preg-replace]: Unknown modifier '$' in C:\wamp\www..Lemuel
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.Christan
You can escape your code with backticks (see stackoverflow.com/editing-help). The $ fix looks good.Devon

© 2022 - 2024 — McMap. All rights reserved.