22

Searching through the internet and this website as well, I've found several topics on the matter. Thing is, there are countless solutions if the inserted strings must contain only characters of the Latin alphabet, but when the case requires text of other alphabets it gets a bit tricky.

Is there any way I can strip in PHP all symbols from a string, but leave the actual letters of all UTF-8 alphabets? I have tried already creating an array of all the characters of my keyboard and then by using str_replace or preg_replace remove them, but then I found out that different countries have also different keyboards sometimes which include different symbols. For example, my qwerty keyboard doesn't have the £ symbol, which a British keyboard might have.

I know this is a weird question, I am just wondering if there is an easy solution to it which I may have missed.

Any help would be very much appreciated!

EDIT: OK After some better and extended Google-ing I have found out that the following regular expression works fine for what I need and it keeps all letters of all types of alphabets while removes all symbols. I am sharing it here in case somebody else would need to do the same.

$string = preg_replace('/[^\p{L}\p{N}\s]/u', '', $string);

5 Answers 5

49

The solution is this: $string = preg_replace('/[^\p{L}\p{N}\s]/u', '', $string);

1
  • 1
    I love the fact that this is UTF-8 compatible. Mar 14, 2016 at 16:24
24
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.

$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

echo $string; // output will be abcdef-g for 'a|"bc!@£de^&$f g'
1
  • 3
    True. But what I need is something to allow all alphabets, not only the Latin one. May 24, 2013 at 11:23
1

Try this:

<?php

$string = "Remove these characters: £äó";
$string = preg_replace('/[^(\x20-\x7F)]*/','', $string);
echo $string;
?>
2
  • 1
    The thing is that the characters äó must not be removed since they are used in words and are not symbols. On the contrary the : should be removed. May 24, 2013 at 11:20
  • Oh! I thought just opposite. Question was about to remove symbols from string :) May 24, 2013 at 11:22
1

As per the other answers, build a regular expression for the characters you permit (eg. from each alphabet you support, and remove other characters. Here's a list of Unicode Block Ranges to get the character values/ranges for each language - that's something you'll need to compile yourself.

1

It is possible to check for unicode characters and numbers, but only if PCRE was compiled with
--enable-unicode-properties. If this is the case then you can use regex \p{Nl} which will match unicode letters and numbers. A lot more information on unicode regex in PHP can be found in the documentation

EDIT: After edit to question

To get symbols use \P instead of \p again look at the PHP documentation I linked to above.

1
  • True my friend. I already found something and I edited my question already. May 24, 2013 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.