I’ve partly figured out how to change my URLs but I’m having trouble with fixing the space(%20) that’s located in my URL.
I’ve tried using the str_replace function but it’s either not working or I’m not doing in correctly.
My goal is to turn this URL (first one below this statement) into the URL that’s below the first one. I’m having a hard time figuring this out. Any help would be appreciated. Thanks Everyone!
My .htaccess code is below as well. Thanks again everyone!
<?php
include('init.php'); // connection to database
// if city exists...
if (isset($_GET['u'])) {
// $city = str_replace(' ','-');
// decode and replace hyphen with space
// $city = str_replace('-','',urldecode($_GET['u']));
// $city = str_replace('%20','-',urldecode($_GET['u']));
$city = str_replace('%20','-',urldecode($_GET['u']));
// $city = str_replace(urldecode($_GET['u']),' ','-');
// $city = str_replace('','-',urldecode($_GET['u']));
// $city = str_replace('-','','%20','-', urldecode($_GET['u']));
// if value contains only letters, numbers or spaces...
if ( preg_match('~^[a-z0-9 ]+$~i',$city) ) {
// select data from database...
$data = mysql_query("SELECT State, City FROM cars WHERE City='$city'" );
if (mysql_num_rows($data) > 0) {
while ($row = mysql_fetch_assoc($data)) {
echo $row["City"].'<br>';
echo $row["State"].'<br>';
echo 'This is what I get when I echo $_GET '.$_GET['u'].'<br>';
}
}
}
}
?>
You’re URLDecoding the variable, which changes “Key%20West” into “Key West” and then doing a str-replace on it looking for %20… but the %20 is already gone, so you end up with “Key West” again.
The removing of the hyphen, or achieving the URL format you now want?
In either case, no, not really. You are doing it right. The only thing I would mention is that if you ever want to replace multiple characters, to use preg_replace() instead of creating a str_replace for each one (especially if you are going to replace each character with a space).
But you are using RewriteRules to do what they were intended, granted, @dklynn ; would give you a very long drawn out rant over the use of (.*), as it is a catch all, when you likely could have used ([a-zA-Z0-9 -]+)