Reputation: 2658
I need to be able to have spaces and parentheses in a URI segment of my site's URLs (the segment is used for DB queries). So, I rawurlencode()
a string and then rawurldecode()
the same string in the receiving page, however it's not entirely the same. The spaces are fine but parentheses are left in their hex char codes. Which completely messes up my database queries. Any idea why rawurldecode
isn't producing the EXACT string which was passed to rawurlencode()
?
the original string = Compton Community College (CA)
the encoded string =
Compton%20Community%20College%20%28CA%29
a vardump of the decoded string is
string(38) "Compton Community College (CA)"
in the view page source
"Compton Community College (CA)"
edit
alright, so apparently this might be a Codeigniter thing (which I'm using). The code
<a href='<?php echo site_url('college/'.rawurlencode($player['college'])); ?>'>
produces the correct url of http://localhost/ff/index.php/college/Compton%20Community%20College%20%28CA%29
The final URI segment is recieved as a parameter in the following function
function view_college($college = FALSE)
{
if($college === FALSE)
{//no college provided
redirect('');
}
$college = rawurldecode($college);
And at this point the string is already in the form displayed above. So maybe I'm screwing something up or just can't expect CI to correctly pass the string. :-( Thoughts?
Upvotes: 1
Views: 1240
Reputation: 107
A little late but happend to me and I found this solution.
The problem is in the _filter_uri function of URI class of CI. This is what they do in that function:
// Convert programatic characters to entities
$bad = array('$', '(', ')', '%28', '%29');
$good = array('$', '(', ')', '(', ')');
return str_replace($bad, $good, $str);
So you have to invert that and then apply rawurldecode:
function view_college($college = FALSE)
{
if($college === FALSE)
{//no college provided
redirect('');
}
$bad = array('$', '(', ')', '(', ')');
$good = array('$', '(', ')', '%28', '%29');
$college = str_replace($bad, $good, $college);
$college = rawurldecode($college);
Upvotes: 0
Reputation: 7388
No No No. Do not do that. What a headache!
Put all of your colleges in a database and use an auto-incrementing integer id
column to drill into it's content:
If you put a HTML <base>
element inside <head>
to define your root:
<base href="http://localhost/yourapp/">
Then you can write it VERY SIMPLY:
<a href='college/<?= $id;?>'><?= $name;?></a>
Will essentially render code that will act like:
<a href='http://localhost/yourapp/college/5'>Some Community College</a>
Update 1: After @Frank Farmer's comment, I advise keeping the auto-incrementing id
field, but retract using this in the URL. Rather, programmatically generate a separate alpha-numeric SEOid and use it instead for search engine optimization. Same basic link construction.
Upvotes: 0
Reputation: 37691
If you don't want to use IDs (for SEO reasons maybe), you can add another field to the db - web url, and write a web-safe version of the college name... In your example it could be something like Compton-Community-College-CA or even lowercase.
Full url:
http://localhost/ff/index.php/college/Compton-Community-College-CA
CodeIgniter's URL Helper has a nice function for that: url_title().
Upvotes: 1