Jump to content

Recommended Posts

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

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.

$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]

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.

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.

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.

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";

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.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.