PHP regex combined with base64_encode()

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
LVL 14
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

can you explain of what exactly you want? Do you want to replace base64_encoded strings?
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>";

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");
hujiAuthor Commented:
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

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

function matches($match) {
       return 'username="'.base64_encode($match[1])."'";
oops mistake again, the corrected one


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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.