?
Solved

PHP regex combined with base64_encode()

Posted on 2006-04-21
5
Medium Priority
?
316 Views
Last Modified: 2012-06-27
Hi
In a text file which I open suing PHP, I want to change all occerences of:
username="some text here"
to such:
username="base64_encode(same text above)"
I'd like to use regular expressions for finding and replacing the strings.
Please guide
Huji
0
Comment
Question by:huji
  • 4
5 Comments
 
LVL 11

Expert Comment

by:star_trek
ID: 16509220
can you explain of what exactly you want? Do you want to replace base64_encoded strings?
0
 
LVL 11

Expert Comment

by:star_trek
ID: 16509460
make sure you have writable permissions to the output file/path($output_file).

In PHP 5
$input_file = "<inp-file>";
$output_file = "<out-file_with_path>";
file_put_contents($output_file,preg_replace("/username=(.*)/","username=".base64_encode('\1'),file_get_contents($input_file)));

IN PHP < 5
$input_file = "<inp-file>";
$output_file = "<out-file_with_path>";
$buff = preg_replace("/username=(.*)/","username=".base64_encode('\1'),file_get_contents($input_file));
$file = fopen("$output_file","w");
fwrite($file,$buff);
fclose($file);
0
 
LVL 14

Author Comment

by:huji
ID: 16510730
It is not returing what I want.
Look at this piece of code:

<?
$input = 'username="abs"  text  <br> username="sdsdf"   text2';
echo preg_replace('/username=\"(.*?)\"/','username="'.base64_encode('\1').'"',$input);
?>

What it reutrns is this:

username="XDE=" text
username="XDE=" text2

XDE= is the base64_encode of the string '\1' and not the matched string.

What I expected was this:

username="YWJz" text
username="c2RzZGY=" text2

0
 
LVL 11

Expert Comment

by:star_trek
ID: 16511855
I didn't reliaze you have two usernames on the same lines, any way you have to use preg_replace_callback, match is the function
preg_replace('/username="(\w+)"/',"matches",$input);

function matches($match) {
       return 'username="'.base64_encode($match[1])."'";
}
0
 
LVL 11

Accepted Solution

by:
star_trek earned 2000 total points
ID: 16511879
oops mistake again, the corrected one

preg_replace_callback('/username="(\w+)"/',"matches",$input);

function matches($match) {
       return 'username="'.base64_encode($match[1])."'";
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month17 days, 4 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question