Page 1 of 1
php ereg_replace script remove backslash for search \141 to
Posted: Sat Mar 04, 2006 10:09 am
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.

Posted: Sat Mar 04, 2006 10:12 am
by feyd
post your existing code.
Posted: Sat Mar 04, 2006 11:21 am
by dub27
feyd | Please use 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
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Sat Mar 04, 2006 11:27 am
by feyd
you're replacing 141 only.
Posted: Sat Mar 04, 2006 11:34 am
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
Posted: Mon Mar 06, 2006 4:13 am
by dub27
Thank you for your help, really appreciate it!