0
votes

I want to replace every "../" and "script/uploaded" in my string variable to "" !

I have one function like this :

public function mypregReplace($v) 
{                          
    return preg_replace(
        array("%script/uploaded%"  , ""),
        array( "[\.\./]"  ,  ""),
        $v); 
}

but it showa me this error

preg_replace() [function.preg-replace]: Delimiter must not be alphanumeric or backslash in

what`s wrong on my pattern?!

2

2 Answers

2
votes

Your format is wrong, and you dont need preg_replace for this. Use str_replace

public function mypregReplace($v) 
{                          
   return str_replace(
    array("script/uploaded"  , "../"),
    '',
    $v); 
}
1
votes

You have your array wrong. The first array should consist only of patterns, and the second should be the replacement values. You want:

public function mypregReplace($v) 
{                          
    return preg_replace(
        array("%script/uploaded%", "%\.\./%"),
        array("",  ""),
        $v); 
}

Which can be further simplified to:

public function mypregReplace($v) 
{                          
    return preg_replace(
        array('%script/uploaded%', '%\.\./%'),
        '',
        $v); 
}

You should be using str_replace for this, however. You don't need regular expressions to match exact strings:

public function mypregReplace($v) 
{                          
    return str_replace(array('script/uploaded', '../'), '', $v); 
}