PHP Error

I just testing this PHP to encode an email but it comes up with an error.

Anyone knows how line 7 should be formatted?

return $encoded;

<?php $email = "email@hidden" ; function encode_email($email) { $encoded = bin2hex($email); $encoded = chunk_split($encoded, 2, '%'); $encoded = '%' . substr($encoded, 0, strlen($encoded) - ); return $encoded; } echo "$encoded"; ?>

Parse error: syntax error, unexpected T_VARIABLE in line 7


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Try making this line (I get line 6 when I paste this in a new
document) like this:

$encoded = '%' . substr($encoded, 0, strlen($encoded)  );

There was a dash in there after the ($encoded) part.

Also curious, why do you have $encoded in quotes on the last line?
They don’t do anything there.

Walter

On Apr 30, 2008, at 12:53 PM, WebWorker wrote:

I just testing this PHP to encode an email but it comes up with an
error.

Anyone knows how line 7 should be formatted?

return $encoded;

<?php $email = "email@hidden" ; function encode_email($email) { $encoded = bin2hex($email); $encoded = chunk_split($encoded, 2, '%'); $encoded = '%' . substr($encoded, 0, strlen($encoded) - ); return $encoded; } echo "$encoded"; ?>

Parse error: syntax error, unexpected T_VARIABLE in line 7


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I found the script here: http://www.phpit.net/code/encode-email/

Thank I get no error now (must check invisibles from copying code from HTML), but now does not seem to encode $email…

I’m assuming the variable $email after this script contains the encoded $email output put in at the top?

If I can get it working .

<?php $email = "email@hidden" ; function encode_email($email) { $encoded = bin2hex($email); $encoded = chunk_split($encoded, 2, '%'); $encoded = '%' . substr($encoded, 0, strlen($encoded) ); return $encoded; } echo "$email"; ?>

dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Try replacing the last line with this (all the code is there, you’re
just not calling it):

echo (encode_email($email));

Walter

On Apr 30, 2008, at 2:00 PM, WebWorker wrote:

I found the script here: http://www.phpit.net/code/encode-email/

Thank I get no error now (must check invisibles from copying code
from HTML), but now does not seem to encode $email…

I’m assuming the variable $email after this script contains the
encoded $email output put in at the top?

If I can get it working .

<?php $email = "email@hidden" ; function encode_email($email) { $encoded = bin2hex($email); $encoded = chunk_split($encoded, 2, '%'); $encoded = '%' . substr($encoded, 0, strlen($encoded) ); return $encoded; } echo "$email"; ?>

dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Mmm… not quite as simple as I thought. The script returns this in a browser.

%6e%61%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%

How does this get back into a functional email address (i.e. text)?

<?php $email = "email@hidden" ; function encode_email($email) { $encoded = bin2hex($email); $encoded = chunk_split($encoded, 2, '%'); $encoded = '%' . substr($encoded, 0, strlen($encoded) ); return $encoded; } echo (encode_email($email)); ?>

dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

If you use that as the address in a mailto: url, you will see that it
works in a browser.

<a href="mailto:%6e%61%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%">Mail  

Me

(that line above will probably be mangled by Mail – it should all be
one line)

Walter

On Apr 30, 2008, at 2:42 PM, WebWorker wrote:

Mmm… not quite as simple as I thought. The script returns this in
a browser.

%6e%61%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%

How does this get back into a functional email address (i.e. text)?

<?php $email = "email@hidden" ; function encode_email($email) { $encoded = bin2hex($email); $encoded = chunk_split($encoded, 2, '%'); $encoded = '%' . substr($encoded, 0, strlen($encoded) ); return $encoded; } echo (encode_email($email)); ?>

dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Walter,

Thanks, yes it did work in a mailto, I was thrown off track by just returning it as plain text. So I right in saying, the browser only converts this when in a href mailto:

One Question…

In the original code @ http://www.phpit.net/code/encode-email/

They have a " -1 " on line 5. What does that do? Should it in there?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

After all that, it does not seem to work.

I’m having lots of Paypal Buttons buttons on a site, and setting the email globally in one page on the site (entered by the user on the server side page), and then getting PHP to Populate each button with this global email - but keeping the button hidden from spambots.

The PHP encoded email does not work when sent through to the PayPal Cart. But if the email is encoded in entities like Freeway it works in the Paypal Cart.

Can PHP encode emails into entities? or any advice for a possible encoded email PHP solution?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I’m not sure. Try adding it. It might be part of the encoding
process, but if you got a good result from the original method, you
could leave it out.

Another way to do this is as follows:

$encrypted = rawurlencode($email);

It’s a native (written in C) method of PHP, so you don’t need to
write a function to do the same thing, which then needs to be
interpreted and run (microscopically longer execution time on a
modern server).

Walter

On May 1, 2008, at 3:53 AM, WebWorker wrote:

Walter,

Thanks, yes it did work in a mailto, I was thrown off track by just
returning it as plain text. So I right in saying, the browser
only converts this when in a href mailto:

One Question…

In the original code @ http://www.phpit.net/code/encode-email/

They have a " -1 " on line 5. What does that do? Should it in there?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

This may be related to the other issue you noted with the missing -1.
Do try the native rawurlencode method first, as it’s both faster and
more expressive (you will have less trouble figuring out what it does
in three months when you look at the code again).

Walter

On May 1, 2008, at 4:32 AM, WebWorker wrote:

The PHP encoded email does not work when sent through to the PayPal
Cart. But if the email is encoded in entities like Freeway it
works in the Paypal Cart.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

If I try the rawurlencode only a @ sign is changed to a %40 the rest of a email remains intact.

Is that enough to protect an email address?

<?php echo 'mail'; ?>

returns mail %40 domain.com


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Oh, sorry, no. My mistake. It won’t fool much of anyone. Let’s try to
get the original form to work correctly. IF you add the -1 back in,
does it do anything different?

Walter

On May 1, 2008, at 9:17 AM, WebWorker wrote:

If I try the rawurlencode only a @ sign is changed to a %40 the
rest of a email remains intact.

Is that enough to protect an email address?

<?php echo 'mail'; ?>

returns mail %40 domain.com


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

The original code:

<?php $email = "mail AT domain.com" ; function encode_email($email) { $encoded = bin2hex($email); $encoded = chunk_split($encoded, 2, '%'); $encoded = '%' . substr($encoded, 0, strlen($encoded) -1 ); return $encoded; } $codedemail = (encode_email($email)); echo "Mail"; ?>

the email is encoded to “%6d%61%69%6c%40%64%6f%6d%61%69%6e%2e%63%6f%6d”

with or without the -1 works OK to display an email in a web page as a mailto.

My problem has extended to posting this encoded email in a Paypal button to the Paypal cart. FW5 entities works OK, but this encoded does not seem to. Unless i’m not formatting the button correctly. :frowning:


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

It might be helpful then to see the code you are using in PayPal –
any special codes redacted, naturally.

It shouldn’t be hard to do the encoding the way Freeway does, it’s
just converting each character to its numerical equivalent and
encoding it as an HTML entity.

function str_to_entities($string){
	$arr = (array) $string;
	foreach ($arr as $key=>$char){
		$arr[$key] = '&#' . ord($char) . ';'
	}
	return implode("",$arr);
}

That will work for ASCII text, you will have to do something more
elaborate for Unicode, but most e-mail addresses are in ASCII anyway.

Walter

On May 1, 2008, at 9:58 AM, WebWorker wrote:

The original code:

<?php $email = "mail AT domain.com" ; function encode_email($email) { $encoded = bin2hex($email); $encoded = chunk_split($encoded, 2, '%'); $encoded = '%' . substr($encoded, 0, strlen($encoded) -1 ); return $encoded; } $codedemail = (encode_email($email)); echo "Mail"; ?>

the email is encoded to “%6d%61%69%6c%40%64%6f%6d%61%69%6e%2e%63%6f%
6d”

with or without the -1 works OK to display an email in a web page
as a mailto.

My problem has extended to posting this encoded email in a Paypal
button to the Paypal cart. FW5 entities works OK, but this
encoded does not seem to. Unless i’m not formatting the button
correctly. :frowning:


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I’ll give that ago

but heres the code for the button

<?PHP echo "
![|1x1](upload://bEhO1cHZ6pqf7FgjLUjjlTtkgIX.gif)
"; ?>

the $email being plain old text that works when sent to the Paypal cart, or the encoded “%6d%61%69%6c%40%64%6f%6d%61%69%6e%2e%63%6f%6d” that does not work in the cart.

So perhaps I should not be using just $email or something else?

Or I try using your suggestion?


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

If you just type your e-mail address in plain text, and then have
freeway encode it, you could copy and paste that in place of $email.
Or try my function (not tested, but it ought to work) and use that.

Walter

On May 1, 2008, at 10:25 AM, WebWorker wrote:

I’ll give that ago

but heres the code for the button


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I’ll have ago with that function.

Can’t encode the email in FW5 as the email is set globally on another server side page by the client.

I’ll see how it works.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Sorry Walter that does not work

<?php $string = "mail AT domain.com" ; function str_to_entities($string){ $arr = (array) $string; foreach ($arr as $key=>$char){ $arr[$key] = '&#' . ord($char) . ';' } return implode("",$arr); } $codedemail = (str_to_entities($string)); echo "Mail"; ?>

Parse error: syntax error, unexpected ‘}’ on line 7

I’m showing my PHP inexperience here… but willing eager to try better.

I apologuise to you Walter for asking again, but it seems you are the be the only one around to offer help. I feel guilty for once again asking after floundering.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Add another semicolon after the one in quotes on the line previous. I
left that off. SB:

$arr[$key] = '&#' . ord($char) . ';';

Walter

On May 1, 2008, at 11:25 AM, WebWorker wrote:

Parse error: syntax error, unexpected ‘}’ on line 7

I’m showing my PHP inexperience here… but willing eager to try
better.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I am truly happy to help where I can. Many of you don’t remember me
from 1997 when I joined this list and asked more questions than I
answered. I was trained by Jesuits (high school) to be a “man for
others” and to give of what I was given. AMDG!

Walter

On May 1, 2008, at 11:25 AM, WebWorker wrote:

I apologuise to you Walter for asking again, but it seems you are
the be the only one around to offer help. I feel guilty for once
again asking after floundering.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options