Solved

PHP Script URLs: Ampersand to &

Posted on 2014-01-23
7
318 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 30

Expert Comment

by:Marco Gasi
Comment Utility
Can you post the code which generates urls?
0
 
LVL 30

Assisted Solution

by:Marco Gasi
Marco Gasi earned 125 total points
Comment Utility
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 82

Accepted Solution

by:
Dave Baldwin earned 125 total points
Comment Utility
There isn't any automatic conversion to '&' in URLs in any version of PHP.  That's something you have to do yourself.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 125 total points
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
Comment Utility
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
Comment Utility
Thanks!!
0
 
LVL 108

Expert Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
lastpass auto fill login form 5 20
Not needed 13 53
mysql left join sentence 7 18
Why does my array not "dump?" 5 4
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now