473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Convert string for use in URLs in multilanguage environment

Sorry for the multipost - I forgot to crosspost and alt.php gets less
attention than comp.lang.php... And I hope this will work with UTF-8.

In order to make strings suitable for URLs in a UTF-8 encoded website, I use
2 functions, the first of which removes accents from some Latin-1, Latin-2,
and Turkish characters (suggestions for changes or additions welcome!), and
the second removes non-word characters by spaces and then urlencode()s the
string:

function remove_accents($string, $german=false) {
// Single characters
$single_fr = explode(" ", "À Á Â Ã Ä Å A A Ç C C D D Ð È É Ê Ë E E G Ì Í
Î Ï I L L L Ñ N N Ò Ó Ô Õ Ö Ø O R R S S S T T Ù Ú Û Ü U U Ý Z Z Z à á â ã ä
å a a ç c c d d è é ê ë e e g ì í î ï i l l l ñ n n ð ò ó ô õ ö ø o r r s s
s t t ù ú û ü u u ý ÿ z z z");
$single_to = explode(" ", "A A A A A A A A C C C D D D E E E E E E G I I
I I I L L L N N N O O O O O O O R R S S S T T U U U U U U Y Z Z Z a a a a a
a a a c c c d d e e e e e e g i i i i i l l l n n n o o o o o o o o r r s s
s t t u u u u u u y y z z z");
$single = array();
for ($i=0; $i<count($single_fr); $i++) {
$single[$single_fr[$i]] = $single_to[$i];
}
// Ligatures
$ligatures = array("Æ"=>"Ae", "æ"=>"ae", "O"=>"Oe", "o"=>"oe",
"ß"=>"ss");
// German umlauts
$umlauts = array("Ä"=>"Ae", "ä"=>"ae", "Ö"=>"Oe", "ö"=>"oe", "Ü"=>"Ue",
"ü"=>"ue");
// Replace
$replacements= array_merge($single, $ligatures);
if ($german) $replacements= array_merge($replacements, $umlauts);
$string = strtr($string, $replacements);
return $string;
}

function make_url_string($string) {
$string = strtolower(remove_accents($string, true));
$string = preg_replace("/([\W]+)/", "-", $string);
return urlencode(trim($string, "-"));
}

I have 2 questions on this:

1. preg_replace("/([\W]+)/", "-", $string); removes all non-ASCII
characters. Is there any possibility to remove only punctuation and such
stuff, but keep all kinds of letters from whatever character sets?

2. Is there a better way to encode strings for URLs? Or is it maybe
inevitable to collect the real name and the name for the url separately to
get an ASCII-only entry?

Thanks for suggestions!

--
Markus
Sep 20 '05 #1
1 2325
Markus Ernst wrote:
2. Is there a better way to encode strings for URLs?


Arguably. Another way is to take your character, encode it as
UTF-8 octets, and percent-encode that encoding. This is the mapping
of IRIs to URIs described in RFC3987. Take <é> (U+00E9). Encoded as
UTF-8 then percent-encoded, you get %C3%A9 (not %E9).

http://host.invalid/caf%C3%A9

--
Jock
Sep 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: David C. Taylor | last post by:
I saw an article that described how to convert the query string into an Url without a query string and return the page. For example, this request: ...
12
by: MARTIN LANNY | last post by:
Hi All, In my program I am loading the content of a text file (main.txt) into a string called 'message'. What I need is to convert all urls and emails in this string into html. This is what...
5
by: Michal Táborský | last post by:
I am wondering, if it's effective to use text arrays to store multilanguage information. We used to do it like this: CREATE TABLE product ( id serial NOT NULL, price float4, ... )
4
by: Nicole | last post by:
Hello, Can anyone tell me or give me an online tool which can help me to convert php dynamic urls to html urls to make a google friendly site.I dont have apache server on my computer and I dont...
3
by: GM | last post by:
Dear all, Could you all give me some guide on how to convert my big5 string to unicode using python? I already knew that I might use cjkcodecs or python 2.4 but I still don't have idea on what...
15
by: AG | last post by:
I have seen the samples for multilanguage support regarding what is normally static content. Can anyone point me to a good sample for multilanguage support for dynamic content. Like a gridview...
1
by: paolob | last post by:
I need to provide multilanguage support to my application. I alredy writed a multilanguage test application, using VS6 and resource only dll. Now i'm using Visual Studio 2005, I find out that it...
3
by: SM | last post by:
Hi, I have a column with SQL Server type binary(20) (contain a sql handle, ex : 0x01000800B73B573B98C6B1030000000000000000) I want to convert this column to string value with this code : ...
7
by: Larry Neylon | last post by:
Hi there, We are in the process of implementing Membership and Role Management and have a problem with the connection strings in our web.config. We currently have a system that on...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.