Rommeo Posted December 25, 2009 Share Posted December 25, 2009 There is some special chars for a multi language website I was using these this for english text ; $text = urlencode(preg_replace('#[^a-z]#i', '', $text)); return $text; But I want to allow some special chars like : "ş,ç,ı,ö,ü"also uppercase of these "ŞÇIÖÜ" How can I do it ? Thank you in advance. Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/ Share on other sites More sharing options...
Daniel0 Posted December 25, 2009 Share Posted December 25, 2009 Set the correct locale using set_locale and use \W. Otherwise just specify the characters manually in your character class. Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984056 Share on other sites More sharing options...
Rommeo Posted December 25, 2009 Author Share Posted December 25, 2009 Set the correct locale using set_locale and use \W. Otherwise just specify the characters manually in your character class. I have made it like : setlocale(LC_ALL, 'tr_TR'); $text = urlencode(preg_replace('#[^a-z]#i', '', $text)); return $text; it does not work yet, by the way what you mean by "\w" ? I m sorry that I m not good about php, I also tried $text = urlencode(preg_replace('#[^a-zşçöü]#i', '', $text)); return $text; and it did not work. Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984072 Share on other sites More sharing options...
Daniel0 Posted December 25, 2009 Share Posted December 25, 2009 $text = urlencode(preg_replace('#\W#', '', $text)); http://php.net/manual/en/regexp.reference.backslash.php Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984077 Share on other sites More sharing options...
Rommeo Posted December 25, 2009 Author Share Posted December 25, 2009 $text = urlencode(preg_replace('#\W#', '', $text)); http://php.net/manual/en/regexp.reference.backslash.php unfortunately it did not work. function setlocale(LC_ALL, 'tr_TR'); $text = urlencode(preg_replace('#\W#', '', $text)); input : çamaşır output : %C3ama%C5%C4r[/code] Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984080 Share on other sites More sharing options...
Daniel0 Posted December 25, 2009 Share Posted December 25, 2009 Seems to work fine for me... What exactly do you want? What did you expect as output? The code does exactly what it says. It'll replace any non-word characters as defined by the tr_TR locale with nothing and then encode the entire URL. Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984082 Share on other sites More sharing options...
Rommeo Posted December 25, 2009 Author Share Posted December 25, 2009 Seems to work fine for me... What exactly do you want? What did you expect as output? The code does exactly what it says. It'll replace any non-word characters as defined by the tr_TR locale with nothing and then encode the entire URL. For example when I enter çamaşır it should give me çamaşır again. ( I'm just trying to remove number from TR string ) so this was the code I was using : $text = urlencode(preg_replace('#[^a-z]#i', '', $text)); return $text; the problem: input : çamaşır output : amar I edited that code now it's : setlocale(LC_ALL, 'nl_NL'); $text = urlencode(preg_replace('#[^a-z]#i', '', $text)); return $text; input : çamaşır output : %C3ama%C5%C4r Since the names can contain TR chars, I just want to remove numbers and leave the rest as they are. Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984090 Share on other sites More sharing options...
Daniel0 Posted December 25, 2009 Share Posted December 25, 2009 Well, that's because you're urlencode'ing it. \W excludes digits as well, so if you want you can go back and replace \d with nothing again. Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984091 Share on other sites More sharing options...
Rommeo Posted December 26, 2009 Author Share Posted December 26, 2009 Well, that's because you're urlencode'ing it. \W excludes digits as well, so if you want you can go back and replace \d with nothing again. noway Daniel0, I tried what you have told me. I removed urlencode, I also used \d after "$text = urlencode(preg_replace('#[^0-9]#i', '', $text));" it does not give me the correct text yet. Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984101 Share on other sites More sharing options...
Daniel0 Posted December 26, 2009 Share Posted December 26, 2009 You're talking a lot about what is incorrect. How about telling me what would be the correct output? Try this: $text = urlencode(preg_replace('#[\W\d]#', '', $text)); Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984104 Share on other sites More sharing options...
Rommeo Posted December 26, 2009 Author Share Posted December 26, 2009 You're talking a lot about what is incorrect. How about telling me what would be the correct output? Try this: $text = urlencode(preg_replace('#[\W\d]#', '', $text)); The correct output is : input : çamaşır output : çamaşır input : çamaşır14 output : çamaşır input : camaŞır111 output : camaŞır I just want to remove numbers and leave the rest as they are. Now I tried : setlocale(LC_ALL, 'tr_TR'); $text = urlencode(preg_replace('#\W\d#', '', $text)); return $text; it gives me : input : çamaşır111 output : %C3%A7ama%C5%9F%C4%B1r111 correct output which must be : çamaşır I just want to remove numbers from "name" field. Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984107 Share on other sites More sharing options...
Brandon_R Posted December 26, 2009 Share Posted December 26, 2009 $result = preg_replace('#[0-9]#', '', $text); Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984134 Share on other sites More sharing options...
Rommeo Posted December 26, 2009 Author Share Posted December 26, 2009 $result = preg_replace('#[0-9]#', '', $text); ok let's say I want to take all the EN chars plus TR special chars and I dont want any chars like "@ < .......... etc" also the numbers so white list would be better for this. Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984167 Share on other sites More sharing options...
cags Posted December 26, 2009 Share Posted December 26, 2009 You're talking a lot about what is incorrect. How about telling me what would be the correct output? Try this: $text = urlencode(preg_replace('#[\W\d]#', '', $text)); The correct output is : input : çamaşır output : çamaşır input : çamaşır14 output : çamaşır input : camaŞır111 output : camaŞır I just want to remove numbers and leave the rest as they are. Now I tried : setlocale(LC_ALL, 'tr_TR'); $text = urlencode(preg_replace('#\W\d#', '', $text)); return $text; it gives me : input : çamaşır111 output : %C3%A7ama%C5%9F%C4%B1r111 correct output which must be : çamaşır I just want to remove numbers from "name" field. As daniel0 already explained, by passing the value through urlencode you will NEVER get çamaşır as an output, you would get %C3ama%C5%9F%C4r, since none of those 'non-english' characters are URL safe. I don't know what characters are included in the locale tr_TR and I also don't know what characters you are getting that you don't want, but the 'longhand' way of doing what your after is simply... $input = "Çamaşır14"; $output = (preg_replace('#[^a-zA-ZşçıöüŞÇIÖÜ]#', '', $input)); echo "$input becomes $output"; Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984178 Share on other sites More sharing options...
Rommeo Posted December 26, 2009 Author Share Posted December 26, 2009 Yess.. Thank you so much for all your help Daniel0 : $text = urlencode(preg_replace('#[\W\d]#', '', $text)); is working actually, I forgot to remove that urlencode, when I remove it, it gives me the correct output cags: yes, that one works also, I tried that function also it gives me the correct output. thank you so much. Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984183 Share on other sites More sharing options...
cags Posted December 26, 2009 Share Posted December 26, 2009 Glad it's working, don't forget to mark the topic as solved 'Mark Solved' (or something similar) button located bottom left of threads you start. Link to comment https://forums.phpfreaks.com/topic/186334-chars-and-special-chars/#findComment-984188 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.