Reputation: 1919
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?!
Upvotes: 0
Views: 3142
Reputation: 7804
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);
}
Upvotes: 2
Reputation: 141877
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);
}
Upvotes: 1