I want to convert any title e.g. of a blog entry to a user friendly url. I used rawurlencode() to do that but it gives me a lot of strange strings like %s
.
The algorithm should consider german chars like Ö, Ä, etc. I want to make a url from title and be able to get the title by decoding the url.
I tried some of this code: http://pastebin.com/L1SwESBn that is provided in some other questions but it seems to be one way.
E.g. HÖRZU.de -> hoerzu-de -> HÖRZU.de
Any ideas?
Go to the Page with the URL you'd like to change. If this is a new page, you first need to save it as a Draft. Under the Title field, you'll see a URL that was generated. Click the Edit button and type in the new slug.
A URL slug refers to the end part of a URL after the backslash (“/”) that identifies the specific page or post. Each slug on your web pages needs to be unique, and they provide readers and search engines alike with information about the contents of a web page or post.
A slug is the part of a URL that identifies a particular page on a website in an easy-to-read form. In other words, it's the part of the URL that explains the page's content. For this article, for example, the URL is https://yoast.com/slug, and the slug simply is 'slug'.
php function createUrlSlug($urlString) { $slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $urlString); return $slug; } echo createUrlSlug('this is the example demo page'); // This will return 'this-is-the-example-demo-page' ?>
function url_title($str, $separator = 'dash', $lowercase = FALSE) { if ($separator == 'dash') { $search = '_'; $replace = '-'; } else { $search = '-'; $replace = '_'; } $trans = array( '&\#\d+?;' => '', '&\S+?;' => '', '\s+' => $replace, '[^a-z0-9\-\._]' => '', $replace.'+' => $replace, $replace.'$' => $replace, '^'.$replace => $replace, '\.+$' => '' ); $str = strip_tags($str); foreach ($trans as $key => $val) { $str = preg_replace("#".$key."#i", $val, $str); } if ($lowercase === TRUE) { $str = strtolower($str); } return trim(stripslashes($str)); }
You want to create slugs, but from experience i can tell you the decode possibilities are limited. For example "Foo - Bar" will become "foo-bar" so how do you then can possibly know that it wasn't "foo bar" or "foo-bar" all along?
Or how about chars that you don't want in your slug and also have no representation for like " ` "?
So you can ether use a 1 to 1 converstion like rawurlencode()
or you can create a Slug, here is an example for a function - but as i said, no reliable decoding possible - its just in its nature since you have to throw away Information.
function sanitizeStringForUrl($string){
$string = strtolower($string);
$string = html_entity_decode($string);
$string = str_replace(array('ä','ü','ö','ß'),array('ae','ue','oe','ss'),$string);
$string = preg_replace('#[^\w\säüöß]#',null,$string);
$string = preg_replace('#[\s]{2,}#',' ',$string);
$string = str_replace(array(' '),array('-'),$string);
return $string;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With