PDA

View Full Version : php reg_replace script remove backslash for search \141 to result a


dub27
03-04-2006, 03:53 PM
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

Any help would be very much appreciated.

Dub

djm0219
03-04-2006, 04:00 PM
If you don't have to use reg_replace try:

$Test = str_replace('\141','a',$Test);

ns1987
03-04-2006, 04:13 PM
I think you typo'd, but the function is preg_replace.

Err, try this:
$string = preg_replace('#\\141#', 'a', $string);

dub27
03-04-2006, 04:37 PM
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.

Script is as follows (using ereg_replace):

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.

ns1987
03-04-2006, 04:54 PM
You never replace \141, just 141. o_0

The line $var1 = '\141'; is never used afterwards, from what I see.

And since this is all you do, str_replace might be better.

GJay
03-04-2006, 04:58 PM
I'm not so experienced with posix REs, but is \ not a special character that needs escaping?
But str_replace really would be a better choice in this situation.

ns1987
03-04-2006, 05:05 PM
I'm not so experienced with posix REs, but is \ not a special character that needs escaping?
But str_replace really would be a better choice in this situation.

It does. (See my preg code)

dub27
03-04-2006, 05:14 PM
The purpose in this instance is for code obfuscation, I am playing with some concepts. So would like to be able to encode and decode from '\141' to 'a' and backwards from 'a' to '\141'.

Yes, backslash is a special character so I tried methods of escaping it in the result and got close but did not get there withe ereg_replace.

So, is preg_replace my only option?

Thank you again for your assistance.

Dub27

ns1987
03-04-2006, 05:22 PM
I still don't see why you don't just use

str_replace('\141', 'a', $string);

???

GJay
03-04-2006, 08:07 PM
my previous reply was aimed at the OP, didn't make that very clear :)

dub27
03-06-2006, 10:11 AM
Thank you for your help, really appreciate it!