tags:

views:

164

answers:

7
+1  Q: 

Url replacing

I'm trying to make some nice urls.

CREATE TABLE IF NOT EXISTS `games` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(200) DEFAULT NULL,
`about` text,
`release_date` int(10) NOT NULL DEFAULT '0'

php

$id = str_replace('_', ' ', $_GET['title']);
$query = mysql_query("SELECT * FROM games WHERE title = '$id'");
$row = mysql_fetch_assoc($query);

if (!mysql_num_rows($query)) exit('This game is not in the database.');

Need some help with replacing characters.

Lets say the title of one of my games is Day of Defeat: Source i would like to access it with this: Day_of_Defeat_Source. How would I do that? Removing colons, -, & and all that with nothing. Now its: Day_of_Defeat:_Source

But If I replace : with _ it will look like: Day_of_Defeat__Source

How can I go around this?

Sorry for my cheap english, maybe and moderator can make this more understanable.

+1  A: 

So replace ':' with '', the empty string -- who says you have to put another underscore there?

Alex Martelli
it wont work. $id = str_replace('_', ' ', $_GET['title']); $id = str_replace(':', '', $_GET['title']);
Uh -- what are those two replaces, with the second one ignoring the first?! The second one obviously has to use $id as the third argument!
Alex Martelli
A: 
$bad = array('&', ':', '-'); //array of characters to remove
$id = str_replace($bad, '', $_GET['title'];
$id = str_replace('_', ' ', $id);
...continue with query...
cOle2
A: 
$addr = "Day of Defeat: Source";
$search = array(" ", ":"); 
$replace = array("_", ""); 
$new_addr = str_replace($search, $replace, $addr);

Each value from $search will be searched for in $addr, and if the case it is found is replaced with corresponding value from $replace.

So in this case you will have " " replaced with "_", and ":" replaced with "", so you will get Day_of_Defeat_Source

Alo you can add any chars to the arrays $search and $replace.

JohnZ
A: 

You could URL-encode it, and then read it using URL-decode.

"Day of Defeat: Source" would be "Day%20of%20Defeat%3A%20Source".

TFM
A: 

I would suggest that no matter what choices you make when replacing characters, you should store the URL title of the game in the database. That will make it much easier for you to look up games by URL, since you wouldn't have to undo the replacement logic each time. It also will allow you to create a nice URL without having to make the substitution process reverse-able (reversable?).

With the reversability out of the way, you can now do this, which removes double-underscores:

$id = str_replace(array(" ", ":"), '_', $_GET['title']);
$doubledUp = strpos($id, "__");
while ($doubledUp !== false) {
    $id = str_replace("__", "_", $id);
    $doubledUp = strpos($id, "__");
}
Jacob
A: 

Try this ..

$string = "Day of Defeat: Source";
$string = preg_replace("/[^a-zA-Z0-9 ]/", " ", $string); //remove all non-alphanumeric
$string = preg_replace('/\s+/', ' ', $string); //more than one spaces to single space
$string = preg_replace('/\s/', '_', $string); //space to an underscore
echo $string;

Hope this helps.

Wbdvlpr
A: 

I wrote a function URL::title() for the Kohana framework a long time ago.

/**
 * Convert a phrase to a URL-safe title. Note that non-ASCII characters
 * should be transliterated before using this function.
 *
 * @param   string  phrase to convert
 * @param   string  word separator (- or _)
 * @return  string
 */
public static function title($title, $separator = '-')
{
 $separator = ($separator === '-') ? '-' : '_';

 // Remove all characters that are not the separator, a-z, 0-9, or whitespace
 $title = preg_replace('/[^'.$separator.'a-z0-9\s]+/', '', strtolower($title));

 // Replace all separator characters and whitespace by a single separator
 $title = preg_replace('/['.$separator.'\s]+/', $separator, $title);

 // Trim separators from the beginning and end
 return trim($title, $separator);
}

View source at Github

Geert