http://domain.name/1-As Low As 10% Downpayment, Free Golf Membership!!!
The above url will report 400 bad request
,
how to convert such title to user friendly good request?
http://domain.name/1-As Low As 10% Downpayment, Free Golf Membership!!!
The above url will report 400 bad request
,
how to convert such title to user friendly good request?
Check out rawurlencode http://www.php.net/manual/en/function.rawurlencode.php
See the first answer here http://stackoverflow.com/questions/2103797/url-friendly-username-in-php
You can use urlencode or rawurlencode... for example Wikipedia do that. See this link: http://en.wikipedia.org/wiki/Ichigo_100%25
that's the php encoding for % = %25
You may want to use a "slug" instead. Rather than using the verbatim title as the URL, you strtolower()
and replace all non-alphanumeric characters with hyphens, then remove duplicate hyphens. If you feel like extra credit, you can strip out stopwords, too.
So "1-As Low As 10% Downpayment, Free Golf Membership!!!" becomes:
as-low-as-10-downpayment-free-gold-membership
Something like this:
function sluggify($url)
{
# Prep string with some basic normalization
$url = strtolower($url);
$url = strip_tags($url);
$url = stripslashes($url);
$url = html_entity_decode($url);
# Remove quotes (can't, etc.)
$url = str_replace('\'', '', $url);
# Replace non-alpha numeric with hyphens
$match = '/[^a-z0-9]+/';
$replace = '-';
$url = preg_replace($match, $replace, $url);
$url = trim($url, '-');
return $url;
}
You could probably shorten it with longer regexps but it's pretty straightforward as-is. The bonus is that you can use the same function to validate the query parameter before you run a query on the database to match the title, so someone can't stick silly things into your database.