Solved

PHP Script URLs: Ampersand to &

Posted on 2014-01-23
7
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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 110

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 110

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

687 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