I have a site that I’m building out with php that will allow for multi language for content. One part of the site will have business listings. I have SEO friendly urls setup to view these listings, so for example I would have a business listing called “A bar down the street”. The url would look like this:
/listing/a-bar-down-the-street
However lets say there is an Arabic version of this listing, then the name would look like this:
شريط أسفل الشارع
How would I make that into the same url format as the English version but in the language it is currently in? When I tried my function on the Arabic version that turns a string into a seo friendly url it comes back empty.
EDIT:
To clarify further, all I’m looking for is a php function that allows me to turn any string into an SEO friendly url no matter what language the site is in.
EDIT PART 2
Below is the function Im using to rewrite the string to a SEO friendly url. Perhaps you can tell me what I need to add to make it language friendly?
public function urlTitle($str,$separator = 'dash',$lowercase = TRUE)
{
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);
$str = preg_replace("#\/#ui",'-',$str);
foreach ($trans AS $key => $val)
{
$str = preg_replace("#".$key."#ui", $val, $str);
}
if($lowercase === TRUE)
{
$str = mb_strtolower($str);
}
return trim(stripslashes($str));
}
I have found similar discussion in an existing SO discussion. It seems that what you are requesting should be possible “out-of-the-box”.
I would recommend looking into your webserver config to see what is the problem, there should not be a difference between seo-friendly English urls and any other url-encodable string.
What webserver are you running?
UPDATE
I see that you are only accepting alphanumeric characters:
I suspect that may filter out any non-a-z characters and cause the empty return. Or, alternatively, you can try to debug your function to see which of the replace condition causes your content to be wiped-out.
What you are encountering here is that URLs by default cannot contain any character, browsers in general use encoding to achieve nice-looking multi language URLs.
See example from link:
Which means that you can no longer filter your post title and only allow url-valid characters, you need to encode the titles and hope that the browser will render them as you would like.
Another option would be to use trans-literation, possibly after detection of a browser which is known to not render the url-encoded special characters.