views:

1125

answers:

7

I've discovered that any time I do the following:

echo '<a href="http://" title="bla">huzzah</a>';

I end up with the following being rendered to the browser:

<a href="http:///" title="bla">huzzah</a>

This is particularly annoying when I link to a file with an extension, as it breaks the link.

Any ideas why this is happening and how I can fix it?

Thanks

Update: For those asking about my exact implementation, here it is. In my troubleshooting I've dumbed it down as much as I could, so please don't mind where I concat plain text to plaintext...

function print_it($item) {
    echo '<div class="listItem clearfix">';
    echo '<div class="info">';
    echo '<span class="title">';
    if(isset($item[6])) {
     echo '<a href="http://" title="">' . 'me' . '</a>';
    }
    echo '</span>';
echo '</div></div>';
}

Update: In response to Matt Long, I pasted in your line and it rendered the same.

Update: In response to Fire Lancer, I've put back in my original attempt, and will show you both below.

echo substr($item[6],13) . '<br>';
echo '<a href="http://' . substr($item[6],13) . '" title="' . $item[0] . '">' . $item[0] . '</a>';

<span class="title">www.edu.gov.on.ca%2Feng%2Ftcu%2Fetlanding.html<br>
<a href="http://www.edu.gov.on.ca%2Feng%2Ftcu%2Fetlanding.html" title="Employment Ontario">Employment Ontario</a></span>

The reason for the substr'ing is due to the URL being run through rawurlencode() elsewhere, and linking to http%3A%2F%2F makes the page think it is a local/relative link.

Update: I pasted the above response without really looking at it. So the HTML is correct when viewing source, but the actual page interprets it with another trailing slash after it.

Solution: This was all a result of rawlurlencode(). If I decoded, or skipped the encoding all together, everything worked perfectly. Something about rawurlencode() makes the browser want to stick a trailing slash in there.

Thanks to everyone for your help. I would vote you up, but apparently I need a higher rep to do that.

+3  A: 

Ive never had that, how ecactly are you echoing the link? All the following should work.

echo '<a href="http://someothersite.com"&gt;Link&lt;/a&gt;';
echo '<a href="anotherpage.php">Some page</a>';
echo '<a href="../pageinparentdir.php">Another page</a>';
etc

edit, since you added the info.

You can't just have http:// as href, even entering that link directly into a html page has that effect. eg:
html:

 <a href="http://" title="bla">huzzah</a>

link (in FF3):

http:///
Fire Lancer
+1  A: 

The error must be elsewhere. echo writes the string, verbatim. No post-processing is done on any part. The additional slash is therefore added elsewhere in your code (prior to passing the string to echo).

Konrad Rudolph
A: 

Do you get the same result if you use double quotes and escape internal double quotes like this?

echo "<a href=\"http://\" title=\"bla\">huzzah</a>";
A: 

If I put that echo command in my PHP code, it outputs "http://" as expected (you can see that in the source of the generated output), but when I then mouse over the link in the resulting page (with IE7), it shows http:///.

My guess is, that that's browser behaviour, because there can't be a http:// link without a host name or IP address (you can't just access the protocol).

BlaM
+2  A: 

Firefox, especially, shows you the html source the way it's seeing it which is rarely the way you've sent it. Clearly something about your link or it's context is making the browser interpret a trailing slash.

I wonder if it's a side effect of the url encoding. If you rawurldecode it will that help. If there are parts of the url that need to stay encoded you could search for the slashes and just put those back.

Eric Goodwin
A: 

As some guys pointed out, 'http://' is not a valid link, so your browser adds the extra slash at the end. To view out it, try a lynx -dump http://yourdomain/yourfile.php (if you are fortunate enough to have a linux) or telnet from your box to your server in port 80, and typing this:

GET /path/file.php HTTP/1.0

and look at the result.

ThoriumBR
A: 

Have you looked into your PHP config settings? It might be magic_quotes_gpc deciding to escape things for you (I've been bitten several times by that setting, especially when working with AJAX/JSON traffic). Try making sure it is off and echoing again (you might need to edit your php.ini file, or add php_flag magic_quotes_gpc off to an .htaccess file in the directory you are working in, depending on your environment).

Brian