# PHP Error

**URL:** https://discourse.softpress.com/t/php-error/1489
**Category:** Dynamo
**Created:** [April 30, 2008, 4:53pm UTC](https://discourse.softpress.com/t/php-error/1489 "2008-04-30T16:53:22Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![WebWorker](https://discourse.softpress.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@WebWorker](https://discourse.softpress.com/u/WebWorker)
#### Post date: [April 30, 2008, 4:53pm UTC](https://discourse.softpress.com/t/php-error/1489/1 "2008-04-30T16:53:22Z")

</div>

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](http://freewaytalk.net/person/options)

---

<div class="post-metadata">

### Author: ![waltd](https://discourse.softpress.com/user_avatar/discourse.softpress.com/waltd/32/2720_2.png) [@waltd](https://discourse.softpress.com/u/waltd)
#### Post date: [April 30, 2008, 5:01pm UTC](https://discourse.softpress.com/t/php-error/1489/2 "2008-04-30T17:01:12Z")

</div>

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](http://freewaytalk.net/person/options)

* * *

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

---

<div class="post-metadata">

### Author: ![WebWorker](https://discourse.softpress.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@WebWorker](https://discourse.softpress.com/u/WebWorker)
#### Post date: [April 30, 2008, 6:00pm UTC](https://discourse.softpress.com/t/php-error/1489/3 "2008-04-30T18:00:07Z")

</div>

I found the script here: [Phpit.net](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](http://freewaytalk.net/person/options)

---

<div class="post-metadata">

### Author: ![waltd](https://discourse.softpress.com/user_avatar/discourse.softpress.com/waltd/32/2720_2.png) [@waltd](https://discourse.softpress.com/u/waltd)
#### Post date: [April 30, 2008, 6:12pm UTC](https://discourse.softpress.com/t/php-error/1489/4 "2008-04-30T18:12:17Z")

</div>

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/](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](http://freewaytalk.net/person/options)

* * *

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

---

<div class="post-metadata">

### Author: ![WebWorker](https://discourse.softpress.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@WebWorker](https://discourse.softpress.com/u/WebWorker)
#### Post date: [April 30, 2008, 6:42pm UTC](https://discourse.softpress.com/t/php-error/1489/5 "2008-04-30T18:42:32Z")

</div>

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](http://freewaytalk.net/person/options)

---

<div class="post-metadata">

### Author: ![waltd](https://discourse.softpress.com/user_avatar/discourse.softpress.com/waltd/32/2720_2.png) [@waltd](https://discourse.softpress.com/u/waltd)
#### Post date: [April 30, 2008, 6:54pm UTC](https://discourse.softpress.com/t/php-error/1489/6 "2008-04-30T18:54:53Z")

</div>

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](http://freewaytalk.net/person/options)

* * *

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

---

<div class="post-metadata">

### Author: ![WebWorker](https://discourse.softpress.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@WebWorker](https://discourse.softpress.com/u/WebWorker)
#### Post date: [May 1, 2008, 7:53am UTC](https://discourse.softpress.com/t/php-error/1489/7 "2008-05-01T07:53:32Z")

</div>

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 @ [Phpit.net](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](http://freewaytalk.net/person/options)

---

<div class="post-metadata">

### Author: ![WebWorker](https://discourse.softpress.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@WebWorker](https://discourse.softpress.com/u/WebWorker)
#### Post date: [May 1, 2008, 8:32am UTC](https://discourse.softpress.com/t/php-error/1489/8 "2008-05-01T08:32:10Z")

</div>

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](http://freewaytalk.net/person/options)

---

<div class="post-metadata">

### Author: ![waltd](https://discourse.softpress.com/user_avatar/discourse.softpress.com/waltd/32/2720_2.png) [@waltd](https://discourse.softpress.com/u/waltd)
#### Post date: [May 1, 2008, 11:48am UTC](https://discourse.softpress.com/t/php-error/1489/9 "2008-05-01T11:48:11Z")

</div>

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/](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](http://freewaytalk.net/person/options)

* * *

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

---

<div class="post-metadata">

### Author: ![waltd](https://discourse.softpress.com/user_avatar/discourse.softpress.com/waltd/32/2720_2.png) [@waltd](https://discourse.softpress.com/u/waltd)
#### Post date: [May 1, 2008, 11:56am UTC](https://discourse.softpress.com/t/php-error/1489/10 "2008-05-01T11:56:01Z")

</div>

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](http://freewaytalk.net/person/options)

---

<div class="post-metadata">

### Author: ![WebWorker](https://discourse.softpress.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@WebWorker](https://discourse.softpress.com/u/WebWorker)
#### Post date: [May 1, 2008, 1:17pm UTC](https://discourse.softpress.com/t/php-error/1489/11 "2008-05-01T13:17:50Z")

</div>

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](http://domain.com)

* * *

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

---

<div class="post-metadata">

### Author: ![waltd](https://discourse.softpress.com/user_avatar/discourse.softpress.com/waltd/32/2720_2.png) [@waltd](https://discourse.softpress.com/u/waltd)
#### Post date: [May 1, 2008, 1:30pm UTC](https://discourse.softpress.com/t/php-error/1489/12 "2008-05-01T13:30:35Z")

</div>

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](http://domain.com)
> 
> * * *
> 
> dynamo mailing list  
> email@hidden  
> Update your subscriptions at:  
> [http://freewaytalk.net/person/options](http://freewaytalk.net/person/options)

* * *

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

---

<div class="post-metadata">

### Author: ![WebWorker](https://discourse.softpress.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@WebWorker](https://discourse.softpress.com/u/WebWorker)
#### Post date: [May 1, 2008, 1:58pm UTC](https://discourse.softpress.com/t/php-error/1489/13 "2008-05-01T13:58:53Z")

</div>

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. ☹

 
* * *
 

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

---

<div class="post-metadata">

### Author: ![waltd](https://discourse.softpress.com/user_avatar/discourse.softpress.com/waltd/32/2720_2.png) [@waltd](https://discourse.softpress.com/u/waltd)
#### Post date: [May 1, 2008, 2:09pm UTC](https://discourse.softpress.com/t/php-error/1489/14 "2008-05-01T14:09:20Z")

</div>

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. ☹
> 
>  
> * * *
>  
> 
> dynamo mailing list  
> email@hidden  
> Update your subscriptions at:  
> [http://freewaytalk.net/person/options](http://freewaytalk.net/person/options)

* * *

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

---

<div class="post-metadata">

### Author: ![WebWorker](https://discourse.softpress.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@WebWorker](https://discourse.softpress.com/u/WebWorker)
#### Post date: [May 1, 2008, 2:25pm UTC](https://discourse.softpress.com/t/php-error/1489/15 "2008-05-01T14:25:30Z")

</div>

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](http://freewaytalk.net/person/options)

---

<div class="post-metadata">

### Author: ![waltd](https://discourse.softpress.com/user_avatar/discourse.softpress.com/waltd/32/2720_2.png) [@waltd](https://discourse.softpress.com/u/waltd)
#### Post date: [May 1, 2008, 2:56pm UTC](https://discourse.softpress.com/t/php-error/1489/16 "2008-05-01T14:56:12Z")

</div>

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](http://freewaytalk.net/person/options)

---

<div class="post-metadata">

### Author: ![WebWorker](https://discourse.softpress.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@WebWorker](https://discourse.softpress.com/u/WebWorker)
#### Post date: [May 1, 2008, 3:07pm UTC](https://discourse.softpress.com/t/php-error/1489/17 "2008-05-01T15:07:02Z")

</div>

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](http://freewaytalk.net/person/options)

---

<div class="post-metadata">

### Author: ![WebWorker](https://discourse.softpress.com/letter_avatar_proxy/v4/letter/w/96bed5/32.png) [@WebWorker](https://discourse.softpress.com/u/WebWorker)
#### Post date: [May 1, 2008, 3:25pm UTC](https://discourse.softpress.com/t/php-error/1489/18 "2008-05-01T15:25:44Z")

</div>

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](http://freewaytalk.net/person/options)

---

<div class="post-metadata">

### Author: ![waltd](https://discourse.softpress.com/user_avatar/discourse.softpress.com/waltd/32/2720_2.png) [@waltd](https://discourse.softpress.com/u/waltd)
#### Post date: [May 1, 2008, 3:28pm UTC](https://discourse.softpress.com/t/php-error/1489/19 "2008-05-01T15:28:38Z")

</div>

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](http://freewaytalk.net/person/options)

---

<div class="post-metadata">

### Author: ![waltd](https://discourse.softpress.com/user_avatar/discourse.softpress.com/waltd/32/2720_2.png) [@waltd](https://discourse.softpress.com/u/waltd)
#### Post date: [May 1, 2008, 3:31pm UTC](https://discourse.softpress.com/t/php-error/1489/20 "2008-05-01T15:31:07Z")

</div>

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](http://freewaytalk.net/person/options)

[Next page](https://discourse.softpress.com/t/php-error/1489.md?page=2)
