Welcome to WebmasterWorld Guest from 35.172.195.49

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Formatting Characters with PHP

Odd language chars need formatting...

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

Senior Member

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 4, 2002
posts:5044
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 21, 2003
posts:1069
votes: 0


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.

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

Senior Member

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 4, 2002
posts:5044
votes: 0


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

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

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 21, 2003
posts:1069
votes: 0


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.
7:09 pm on Aug 31, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 26, 2003
posts:881
votes: 0


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

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

Junior Member

10+ Year Member

joined:Oct 8, 2002
posts:65
votes: 0


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...

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

Senior Member

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 4, 2002
posts:5044
votes: 0


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

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

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


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?
7:44 pm on Sept 1, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Feb 4, 2002
posts:5044
votes: 0


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

Cheers!

Nick