• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 658
  • Last Modified:

urlencode showing + instead of %20

New to PHP, so please bare with me...

I am trying to display a variable with "%20" instead of space, but code is just producing a "+" symbol

Example I have a product code "80000 #ABC" and I need to display it as "80000%20%23ABC" to ensure some of my javascript code works correctly
My current code <?= urlencode($product['prod_code']) ?> just produces "80000+%23ABC"
0
fgict
Asked:
fgict
  • 2
2 Solutions
 
marchentCommented:
urlencode doesn't convert space into %20, it just make it +
You can use rawurlencode() to convert space into %20. or use below code.
<?php
    $product['prod_code'] = "80000 #ABC";
    $product['prod_code'] = urlencode( $product['prod_code'] );
    $product['prod_code'] = preg_replace('/\+/', '%20', $product['prod_code'] );
    print $product['prod_code'] . "\n";
?>

Open in new window

0
 
cr4ck3rj4ckCommented:
Hey there,

Try using this,
http://uk3.php.net/rawurlencode

Hope that helps,
CJ
0
 
fgictAuthor Commented:

Ok, Looks like that code will work, however, in my example i'm still having problems, the button for Tell a Friend shows the correct code now:

<li class="send"><a href="javascript:open_popup('80000%20%23ABC')" onMouseOver="self.status='Send to a friend'; return true;">Send to a friend</a></li>
It then loads the sendtoafriend.php popup window with path /sendtoafriend.php?prod_code=80000#ABC perhaps the javascript is throwing it off? see code on product page here:
<script language="JavaScript" type="text/javascript">
<!--
function open_popup(product_code) {
    window.open("sendtoafriend.php?prod_code="+product_code,"Send_to_a_friend","menubar=no,width=440,height=380,toolbar=no");
}
//-->
</script>

Or maybe it is the Send to a Friend Form (sendtoafriend.php) itself, see code on top of this page here:
<?php
// Record what product we're linking to.
$product_code = rawurlencode($_GET['prod_code']);
?>

0
 
marchentCommented:
you code is working perfectly with me.
<script language="JavaScript" type="text/javascript">
<!--
function open_popup(product_code) {
   window.open("sendtoafriend.php?prod_code="+product_code,"Send_to_a_friend","menubar=no,width=440,height=380,toolbar=no");
}
//-->
</script>
<li class="send"><a href="javascript:open_popup('80000%20%23ABC')" onMouseOver="self.status='Send to a friend'; return true;">Send to a friend</a></li>

Open in new window

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now