Forum Moderators: coopster

Message Too Old, No Replies

Formatting Characters with PHP

Odd language chars need formatting...

         

Nick_W

5:10 pm on Aug 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

I have to format some non-english names that contain 'strange characters': I've been looking, but have not found, the correct PHP aproach.

Ideally I'd like to do this:

$oddchars=return_not_so_odd_chars($oddchars);

Any pointers/ideas?

Many thanks..

Nick

justageek

6:51 pm on Aug 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are many ways to do it if I understand you right. The simplest being $notsooddname=str_replace('oddcharacter','',$oddchars);

That'll remove one odd character easy enough. If you need to do more then do something like preg_replace("/[a][\s]+/","\\1",$str); which will remove every letter a and replace it with a space. So just change it to suit your needs.

Nick_W

6:53 pm on Aug 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I was unclear:

Ideally I'd like to change the high chars to low chars: like: ø becomes o etc. I would like to use the names in urls and filenames, so need to convert them....

Thanks!

Nick

justageek

7:01 pm on Aug 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm. Well, I haven't done anything quite like that but if you wanted to you could create a function that has all the mapping in it (because you did it yourself manually) and then do a loop to replace each character to the map. I know, not a very good answer, but it would work.

MonkeeSage

7:09 pm on Aug 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've done similar in JS...converting from English characters to Greek. What I did was make two arrays, one with each alphabet in correlated order (not natural order), then step through the string to convert char by char and for each char I would get the position of it in the English array then build a new string by adding the char from that position in the other array. Sounds like you could do similar and get the position of the high char in the first array and build the new string from the char at that position in the second array of lows. Not sure of the logic of how to do it other than that.

Jordan

Asandir

12:59 am on Sep 1, 2003 (gmt 0)

10+ Year Member



You could use a single call to preg_replace... as preg_replace will allow arrays for the search and replace strings.

Eg,

$find = array ("/a/","/b/","/c/");
$replace = array ("x","y","z");

$within_this_string = preg_replace($find,$replace,$within_this_string);

Then "abcdefg" becomes "xyzdefg".... etc.

I don't know how fast this would be compared to multiple calls to str_replace...

Nick_W

8:24 am on Sep 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks guys. What's really bugging me about this is that recently I skimmed over some function that did exactly this!

I've looked all through the string functions in the manual and cannot find anything...

Nick

coopster

2:12 pm on Sep 1, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I've seen this process done similarly combining the strtr Translate certain characters [us3.php.net] with the utf8_encode [us2.php.net] function. Maybe this is what you were thinking of, Nick?

Nick_W

7:44 pm on Sep 1, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh no, another night scratching my head and swearing at my code ;)

Cheers!

Nick