Hi need to change the function ereg_replace("[\]", "", $theData) to preg_replace
            Asked
            
        
        
            Active
            
        
            Viewed 1.4k times
        
    10
            
            
         
    
    
        codaddict
        
- 445,704
- 82
- 492
- 529
 
    
    
        user359187
        
- 2,269
- 6
- 29
- 44
4 Answers
22
            To port ereg_replace to preg_replace you need to put the regex between a pair of delimiter 
Also your regx is [\] is invalid to be used for preg_replace as the \ is escaping the closing char class ]
The correct port is
preg_replace('/[\\\]/','',$theData) 
Also since the char class has just one char there is no real need of char class you can just say:
preg_replace('/\\\/','',$theData) 
Since you are replace just a single char, using regex for this is not recommended. You should be using a simple text replacement using str_replace as:
str_replace('\\','',$data);
 
    
    
        codaddict
        
- 445,704
- 82
- 492
- 529
- 
                    1'/\\\/' will lead into escaping the forward slash by preg_replace, you need 4 backslashes – Yanick Rochon Sep 06 '10 at 07:12
- 
                    @Yanick, no it won't. `preg_replace` sees it as /\\/, which it decodes as a literal backslash within delimiters. Note that '/\\\\/' is *also* correct, because \\ and \ can both encode a backslash in a string literal. Note that \/ is not a string escape. – Matthew Flaschen Sep 06 '10 at 07:23
- 
                    A better way to explain why is because preg_replace knows the last / is a delimiter rather than a character in pattern body, so it sees 3rd backslash as a literal character. Then we know "/\\\n/" doesn't equal to "/\\\\n" since 3rd backslash will escape n. – Scott Chu Jul 14 '12 at 17:17
2
            
            
        str_replace("\\","",$theData);
But I seriously doubt you need that replace at all. most likely you need some other operation.
What is this replace for?
 
    
    
        Your Common Sense
        
- 156,878
- 40
- 214
- 345
0
            
            
        I used this sed to automatically replace ereg_replace by preg_replace and put the required slashes in. Assumes no \" in the first regex
 sed -i 's#ereg_replace("\([^"]*\)"#preg_replace("/\1/"#g' *.php
 
    
    
        zzapper
        
- 4,743
- 5
- 48
- 45
