1

Possible Duplicate:
How can I convert ereg expressions to preg in PHP?

I have upgraded php and now im getting the ereg_replace deprecated errors.

I have done some searching round web and found that I can use preg instead but not sure how to change this code correctly

$scriptName = ereg_replace(
    "^".$_SERVER["DOCUMENT_ROOT"], "", 
    $_SERVER["SCRIPT_FILENAME"]
);
Community
  • 1
  • 1

2 Answers2

6

Replace the e with a p.

Add a delimiter to the beginning and end of that first argument. Traditionally, people use slashes (/), but I like to use ~ as there is less chance of actually using that character in the regular expression.

sdleihssirhc
  • 42,000
  • 6
  • 53
  • 67
  • No. That'll not do it. `preg_replace` expects delimiters, whereas `ereg_replace` doesn't. You should rephrase that answer a bit. – Linus Kleen Feb 10 '11 at 17:51
  • 3
    `prpgrpplacp` errors out on my machine. :) – Tim Pietzcker Feb 10 '11 at 18:16
  • @Tim That means you have your PHP configured wrong. Open up php.ini in a text editor and find the following line: `la_disparition=` Set it equal to `p`. If you're on a shared host, you can do it on the fly by calling `gadsby('p')`. – sdleihssirhc Feb 10 '11 at 18:21
1

Just adding delimiters won't work when special characters are included in $_SERVER["DOCUMENT_ROOT"]'s value. You need to escape them as follows:

$scriptName = preg_replace(
  "/^".preg_quote($_SERVER["DOCUMENT_ROOT"],"/")."/",
  "", 
  $_SERVER["SCRIPT_FILENAME"]
);
Highly Irregular
  • 38,000
  • 12
  • 52
  • 70