Warning: preg_replace(): No ending delimiter '/' found

UnleashDchaos picture UnleashDchaos · May 11, 2015 · Viewed 25.1k times · Source

Warning: preg_replace(): No ending delimiter '/' found in C:\wamp\www\upload\upload_demo.php on line 77 I used preg_replace() to replace slash to backslash. But it shows the above warning. Here is the code..

function del_file($file) {
$delete = @unlink($file);
clearstatcache();
if (@file_exists($file)) {
    $filesys = preg_replace("/", "\\\\", $file);
    $delete = @system("del $filesys");
    clearstatcache();
    if (@file_exists($file)) {
        $delete = @chmod($file, 0775);
        $delete = @unlink($file);
        $delete = @system("del $filesys");
    }
}

Answer

pavel picture pavel · May 11, 2015

Use str_replace, or add delimiters to pattern if you really need preg_replace.

$filesys = str_replace("/", "\\\\", $file);

OR

$filesys = preg_replace("~/~", "\\\\", $file);