php ereg_replace script remove backslash for search \141 to

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dub27
Forum Newbie
Posts: 3
Joined: Sat Mar 04, 2006 10:08 am

php ereg_replace script remove backslash for search \141 to

Post by dub27 »

I have searched and searched and tried and tried...

Can anyone provide assistance with a script as follows:

PHP code
Search and Replace using reg_replace
Search on '\141' that is with a backslash in the search variable
Result to 'a' that is alphanumeric replacement

The reg_replace is removing the backslash in the search retrieval but is placing a backslash in the search replace e.g. '\a'

I have tried using single quotes, double quotes, many variations but cannot find a reg_replace expression to remove the backslash and not include in the result search.:o
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

post your existing code.
dub27
Forum Newbie
Posts: 3
Joined: Sat Mar 04, 2006 10:08 am

Post by dub27 »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi, thanks for the response...

Purpose of the script is for a search and replace utility in php. Didn't want to use tools in the market, wanted something I could adapt... I will adapt it further into array, etc.  Playing with code obfuscation and general utility functions...

Script is as follows (using ereg_replace):

Code: Select all

function convert($fileName) 
{ 
// get contents of the file into a string 
$fd = fopen ($fileName, "r"); 
$contents = fread ($fd, filesize ($fileName)); 
fclose ($fd); 
if (ereg("\141",$contents)) 
{ 
echo ".........Found in $fileName<br>"; 

$var1 = '\141';
$contents = ereg_replace('141','a',$contents);
$save = 1; 
} 
else 
$save = 0; 
if ($save) 
{ 
$fd = fopen ($fileName, "w"); 
@flock($fd,"LOCK_EX"); // lock file 
@fwrite ($fd,$contents); // write to file 
@flock($fd,"LOCK_UN"); // release lock 
fclose ($fd); 
echo ".........Replaced in $fileName<br>"; 
} 
return true; 
}
---
The result always returns with '\a' even if the original find is '141' or '\141'. So the query is removing the backslash but is replacing it in the search result.


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're replacing 141 only.
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

Yeah, I was wondering, I couldn't reproduce the problem, figured you might need to escape the \ but don't.

Also, you really don't need to use regex here... I'd just use

Code: Select all

$contents = str_replace("\141", "a", $contents);
Can also use strpos: http://www.php.net/strpos instead of ereg
dub27
Forum Newbie
Posts: 3
Joined: Sat Mar 04, 2006 10:08 am

Post by dub27 »

Thank you for your help, really appreciate it!
Post Reply