Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Script URLs: Ampersand to &

Posted on 2014-01-23
7
Medium Priority
?
346 Views
Last Modified: 2014-02-07
We have a script that uses PHP 4.  All the URLs are generated with the actual ampersand character "&".  We are trying to figure out a way to get these to become the code for an ampersand "&" but cant seem to make it work.  Is there any guide to achieving this?

Thanks!
0
Comment
Question by:oaktrees
7 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39805786
Can you post the code which generates urls?
0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 500 total points
ID: 39805793
You only need to use str_replace:

$str = "Thelma&Louise";
echo htmlspecialchars(str_replace("&", "&", $str));

Open in new window


I used htmlspecialchars only to show you that the replacement has been done. Usually you'll don't have to use it and when you'll print out the string you see only the '&' displayed as '&'
0
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 39805868
There isn't any automatic conversion to '&' in URLs in any version of PHP.  That's something you have to do yourself.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39805922
if you actually wanted to use & as part of the VALUES in a url, you need to use urlencode function on the value before putting it to the url string:
http://lu1.php.net/urlencode
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 39806287
The ampersand has special meaning in the URL.  It's used as an argument separator.  In this role it must be a clear-text ampersand.

//site.com?color=red&size=small&price=low

When you want to embed the ampersand as a data element (perhaps in an XML document?) you "entitize it" to turn it into &.  See these links:

See http://www.laprbass.com/RAY_temp_foo.php and use view source to see the effect.  The browser will render the ampersand and the & exactly the same.  You must use view source to detect the difference.

<?php // RAY_temp_foo.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28347143.html
// DEMONSTRATE HTMLEntities()

$str = 'left&right';
echo $str;
echo PHP_EOL;

$new = htmlentities($str);
echo $new;
echo PHP_EOL;

Open in new window

HTH, ~Ray
0
 

Author Closing Comment

by:oaktrees
ID: 39841746
Thanks!!
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39841823
Thanks for using EE, and thanks for the points! ~Ray
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question