?
Solved

PHP Script URLs: Ampersand to &

Posted on 2014-01-23
7
Medium Priority
?
338 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 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 83

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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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

777 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