Log in

View Full Version : Resolved Looking for an alternative to the + symbol



james438
01-21-2013, 07:49 AM
I use tags for my articles. If you click on a tag it will display all of the articles that has that particular tag. As the number of articles increased I thought it might be useful to be able to narrow results by more than one tag as declared in the url. Fro example .php?category=css+php. The problem is that I now have an article with the tag c++.

I now need to try to come up with some alternative to "+". I am wondering if anyone has any ideas. Here are a few that I have thought of. I could use the tilde symbol. I could separate the terms with a string of letters that are unlikely to be used in a tag. I could change the "+" so that when that particular tag is clicked on it will read as "plus" instead of "+", but only read that way when you see the url in the address bar. That last idea sounds the best, but I was wondering if anyone else had come across this problem and what you did instead.

I remember another member on this forum a few years back working really hard to come up with a way to get the "+" to work and if I remember correctly I tried coming up with some pcre as well, but in the end it was decided that an alternative to the "+" would have to be used.

djr33
01-21-2013, 08:00 AM
What's urlencode('+')? And could you just use that for C++?

james438
01-21-2013, 04:38 PM
The url encode value for + is %2B. I have tried several ways to get it to work, but with no luck so far. It just keeps getting translated as + instead. I want to try a few more things first, because I think I can get your suggestion to work. The space is being translated correctly so I figure the + can be made to work as well.

jscheuer1
01-21-2013, 05:23 PM
If you submit a form:


<form action="#">
<input type="hidden" name="category" value="c++"> <input type="submit" value="c++">
</form>

This is the resulting URL:

.php?category=c%2B%2B#

Some browsers do not automatically add the empty hash, which is inconsequential. If you have a real URL as the action it won't happen, the action can even have its own non-empty hash.

You can style a submit button to look like a link, set the action to whatever page you want it to go to, and you're all set.

I just checked. In IE it loses its ClearType anti-aliasing if you style the font, so you might want to use a javascript assist to have an ordinary link submit the form, at least for IE. Or - if you don't mind having a button, use it as a submit button with it's value set as desired, as in the above.

Or - this works out rather well cross browser, if you can live with the font:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
form.link {
margin: 0;
padding: 0;
display: inline;
}
form.link input {
background-color: transparent;
border-width: 0;
cursor: pointer;
}
</style>
</head>
<body>
<form class="link"action="#">
<input name="category" type="submit" value="c++">
</form>
</body>
</html>

However, I just checked again, this works just fine:


<a href="whatever.php?category=c%28%28">c++</a>

What kind of problem were you having? If the URL is script generated, all you should have to do is either encodeURL() or encodeURLComponent() it.

james438
01-21-2013, 05:56 PM
Thank you. I did not know that and was wondering why it was not working.

One thing I was just reading that I want to try out is the php function urlencode.


<?php
$u="c++";
echo '<a href="mycgi?foo=', urlencode($u), '">c++</a>';
?>

I think djr33 may have been alluding to that, but I wasn't putting what he said together fast enough. Both of your suggestions sound equally useful though.

james438
01-21-2013, 06:30 PM
I have finished reading through your edits and have been trying them out. They, of course, work great. I was using + in my address to separate category tags and then exploding the various tags into an array. The tags had been escaped for security reasons. I still need to look into how that affects url encoding.

What was happening was I was using
<a href="whatever.php?category=c%2B%2B">c++</a> and it was being expressed as whatever.php?category=c++ when I clicked on the link. Since I am unable to repeat that error I am thinking that it was the result of an error in my debugging. I think I was using html entities before djr33's response. I am not sure what I was doing wrong after his response though.

I want to try a few of the things mentioned out and let you know what I discover.

djr33
01-21-2013, 06:37 PM
Sorry if my post wasn't clear. I'm not certain it will work just using urlencode() because then you'll still potentially have ambiguous +'s in the URL. But you could also do some other kind of encoding before urlencode() as needed.

In short, why not keep your current system but change C++ to some other format? Unless you need it to be obvious in the URL, I think that's the easiest answer.

james438
01-21-2013, 07:00 PM
I have something in place that works now. I want it to be fairly intuitive how the url system works so that users can easily edit the url from category=php to category=php+css. Originally the thought was to replace either the + in c++ or the separating symbol of + in php+css.

Keyboard1333 is writing an article on my website and he created the tags c++ and cmd. If someone were to search for cmd+c++ or c+++cmd (both of which will return the same results) the following url will now work:

http://www.animeviews.com/display.php?category=c%2B%2B+cmd

as opposed to the old error prone url of http://www.animeviews.com/display.php?category=c+++cmd

By the way, the solution I am using is str_replace ;) I am thankful in that I have learned more about input types and the ph function urlencode.