0

im struggling with program, that deletes all characters, and left only a-z chars and polish chars: ą, ć, ę, ł, ń, ó, ś, ź, ż. So what i want to do is delete all chars like spaces, dots, numbers, those fancy non printable characters, etc. and create another string with what's left. Thanks in advance.

Example:
Input: "111',///[asdżźć';p],,."
Output: "asdżźćp"
Chwistuniu
  • 13
  • 3
  • A `preg_replace_all` would do the trick. So you want to delete ANY character except for plain a-z? – Javid May 04 '21 at 00:05
  • a-z and polish chars like ó,ż,ź,ć, and i don't feel like specifying all possible chars, especially non printable chars, to cut out is a good idea, or maybe i misunderstood how preg_replace works. – Chwistuniu May 04 '21 at 00:07
  • You need to define exactly which characters you want to remain. PHP (and most of the readers here) won't know what _"Polish characters like..."_ means – Tangentially Perpendicular May 04 '21 at 00:12
  • I specified four examples, but i understand the point. Here is a list of polish chars: ą, ć, ę, ł, ń, ó, ś, ź, ż. – Chwistuniu May 04 '21 at 00:14
  • @Chwistuniu is it lowercaps only or also uppercaps? – Mihail Minkov May 04 '21 at 00:15
  • Only lowercaps. – Chwistuniu May 04 '21 at 00:15
  • That should show you how to remove characters. Then just update characters in character class. – user3783243 May 04 '21 at 00:42
  • @Javid It is just `preg_replace`, only `preg_match` has the `all` variation. – user3783243 May 04 '21 at 00:43
  • 1
    @user3783243 yes, thank you, this works well, I don't know how to mark your answer as correct, but it solves the problem. – Chwistuniu May 04 '21 at 01:02
  • Duplicate https://stackoverflow.com/questions/24676691/whats-a-good-regex-to-include-accented-characters-in-a-simple-way/48925757 – Artier May 04 '21 at 01:39
  • So after some searching, i made this: ```$text = mb_strtolower($text); $txt_search = preg_replace('/[^a-z0-9ąćęłńóśźż]/iu',"", $text);``` , and it may not be correct usage but works, so thats ok for me. The solution from link is good if you want to allow more chars for specific languages like russian(because it leaves chars like latin tilde), and for strings with lower and upper case letters. – Chwistuniu May 04 '21 at 01:49

0 Answers0