phpregexpreg-replaceereg-replace

replacement for ereg_replace


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"]
);

Solution

  • 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"]
    );