Solved

PHP Script URLs: Ampersand to &

Posted on 2014-01-23
7
325 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 125 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 83

Accepted Solution

by:
Dave Baldwin earned 125 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 125 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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 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 109

Expert Comment

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

860 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