By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,173 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,173 IT Pros & Developers. It's quick & easy.

base64 encoding a replace string

P: n/a
I've got the example below to set up phpOpenTracker to log exit URL's
but I'm having trouble getting it to work. I have played with the
quotes and changed the \\2 to $3 and got the url in there but I can't
get it to base64 encode it.

I'm new to PHP and any help on getting the encoding to work would be
appreciated.

<?php
function encode_exit_urls($buffer) {
return preg_replace(
"#<a(.*)href=(\"|')http://([^\"']+)(\"|')#isUm",
'"<a href=\"exit.php?url=".base64_encode(\'\\2\')."\""' ,
$buffer
);
}

// start output-buffering
ob_start('encode_exit_urls');

// main code of your script
?>
--
Regards - Rodney Pont
The from address exists but is mostly dumped,
please send any emails to the address below
e-mail ngps07 (at) infohit (dot) fsnet (dot) co (dot) uk
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Rodney Pont <sp****@infohit.fsnet.co.uk> wrote:
I'm new to PHP and any help on getting the encoding to work would be
appreciated.

<?php
function encode_exit_urls($buffer) {
return preg_replace(
"#<a(.*)href=(\"|')http://([^\"']+)(\"|')#isUm",
'"<a href=\"exit.php?url=".base64_encode(\'\\2\')."\""' ,
$buffer
);
}


You'll have to use preg_replace_callback if you want to "do things" with
the backreferences.

--

Daniel Tryba

Jul 17 '05 #2

P: n/a
On Thu, 23 Oct 2003 12:05:13 +0000 (UTC), Daniel Tryba wrote:
Rodney Pont <sp****@infohit.fsnet.co.uk> wrote:
I'm new to PHP and any help on getting the encoding to work would be
appreciated.

<?php
function encode_exit_urls($buffer) {
return preg_replace(
"#<a(.*)href=(\"|')http://([^\"']+)(\"|')#isUm",
'"<a href=\"exit.php?url=".base64_encode(\'\\2\')."\""' ,
$buffer
);
}


You'll have to use preg_replace_callback if you want to "do things" with
the backreferences.


I'm putting this before any output from HTML and it is changing the URL
but not doing a base64 encode on it. Should the base64 encode be done
when it does the replacement or is that something done by the browser
when it goes to the link?

I've tried <a href="exit.php?url=.base64_encode('the_link_url'). ">
directly in the link so I don't think the browser does it.

The problem is that I have some links to Google articles and unless I
encode them exit.php sees them as parameters to it and not to Google.

I've given up on this for the site, I'm base64 encoding the URL and
pasting it into the href, but I'm interested in how this is supposed to
work. I can't find any info on base64_encode being embedded in a string
in the PHP manual.

--
Regards - Rodney Pont
The from address exists but is mostly dumped,
please send any emails to the address below
e-mail ngps07 (at) infohit (dot) fsnet (dot) co (dot) uk
Jul 17 '05 #3

P: n/a
Rodney Pont <sp****@infohit.fsnet.co.uk> wrote:
<?php
function encode_exit_urls($buffer) {
return preg_replace(
"#<a(.*)href=(\"|')http://([^\"']+)(\"|')#isUm",
'"<a href=\"exit.php?url=".base64_encode(\'\\2\')."\""' ,
$buffer
);
}
You'll have to use preg_replace_callback if you want to "do things" with
the backreferences.

[snip] I've given up on this for the site, I'm base64 encoding the URL and
pasting it into the href, but I'm interested in how this is supposed to
work. I can't find any info on base64_encode being embedded in a string
in the PHP manual.


Have you actually read my posting? You have to use preg_replace_callback()
http://php.net/preg_replace_callback instead of preg_replace()

It takes a funtion instead of a string. That function should return the
a/href with the backreference 2 base64 encoded.

--

Daniel Tryba

Jul 17 '05 #4

P: n/a
On Fri, 24 Oct 2003 11:39:15 +0000 (UTC), Daniel Tryba wrote:
<?php
function encode_exit_urls($buffer) {
return preg_replace(
"#<a(.*)href=(\"|')http://([^\"']+)(\"|')#isUm",
'"<a href=\"exit.php?url=".base64_encode(\'\\2\')."\""' ,
$buffer
);
}

You'll have to use preg_replace_callback if you want to "do things" with
the backreferences.

[snip]
I've given up on this for the site, I'm base64 encoding the URL and
pasting it into the href, but I'm interested in how this is supposed to
work. I can't find any info on base64_encode being embedded in a string
in the PHP manual.


Have you actually read my posting? You have to use preg_replace_callback()
http://php.net/preg_replace_callback instead of preg_replace()

It takes a funtion instead of a string. That function should return the
a/href with the backreference 2 base64 encoded.


I did read your posting Daniel. I looked up preg_replace_callback but I
didn't understand how to create a function that could replace the
a/href base64 encoded.

Having had another look I think the light is beginning to dawn. If I
understand it correctly all I have to do with the function is do a
base64 encode on the third subpattern match, add the bits I want on and
return it.

I don't have output buffering on the host so I'm having to turn it on
to create a session cookie and then turn it off. This works fine with
Apache here but inserts a blank line with IIS on the host when I turn
it off and flush, this isn't helping with the formatting :-)

Thanks for your help and patience.

--
Regards - Rodney Pont
The from address exists but is mostly dumped,
please send any emails to the address below
e-mail ngps07 (at) infohit (dot) fsnet (dot) co (dot) uk
Jul 17 '05 #5

P: n/a
Rodney Pont <sp****@infohit.fsnet.co.uk> wrote:
Have you actually read my posting? You have to use preg_replace_callback()
http://php.net/preg_replace_callback instead of preg_replace()

It takes a funtion instead of a string. That function should return the
a/href with the backreference 2 base64 encoded.
I did read your posting Daniel. I looked up preg_replace_callback but I
didn't understand how to create a function that could replace the
a/href base64 encoded.

Having had another look I think the light is beginning to dawn. If I
understand it correctly all I have to do with the function is do a
base64 encode on the third subpattern match, add the bits I want on and
return it.


:) The online manual sometimes needs to be read more than once...
I don't have output buffering on the host so I'm having to turn it on
to create a session cookie and then turn it off. This works fine with
Apache here but inserts a blank line with IIS on the host when I turn
it off and flush, this isn't helping with the formatting :-)


Hmmm, have you taken a look in the bug database? Sounds weird.

--

Daniel Tryba

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.