如何从字符串中修剪特殊字符?

时间:2023-01-06 11:11:04

I want to remove all non-alphanumeric signs from left and right of the string, leaving the ones in middle of string.

我想从字符串的左侧和右侧删除所有非字母数字符号,将其留在字符串的中间。

I've asked similar question here, and good solution is:

我在这里问了类似的问题,好的解决方案是:

$str = preg_replace('/^\W*(.*\w)\W*$/', '$1', $str);

But it does remove also some signs like ąĄćĆęĘ etc and it should not as its still alphabetical sign.

但它确实删除了一些像ĄćĆĆ等等的标志,它不应该作为其仍然按字母顺序排列的标志。

Above example would do:

上面的例子会做:

~~AAA~~  => AAA (OK)
~~AA*AA~~ => AA*AA (OK)
~~ŚAAÓ~~  => AA (BAD)

2 个解决方案

#1


4  

Make sure you use u flag for unicode while using your regex.

确保在使用正则表达式时使用u标志作为unicode。

Following works with your input:

以下处理您的输入:

$str = preg_replace('/^\W*(.*\w)\W*$/u', '$1', '~~ŚAAÓ~~' );

// str = ŚAAÓ

But this won't work: (Don't Use it)

但这不起作用:(不要使用它)

$str = preg_replace('/^\W*(.*\w)\W*$/', '$1', '~~ŚAAÓ~~' );

#2


3  

You can pass in a list of valid characters and tell the function to replace any character that is not in that list:

您可以传入有效字符列表并告诉函数替换该列表中不存在的任何字符:

$str = preg_replace('/[^a-zA-Z0-9*]+/', '', $str);

$ str = preg_replace('/ [^ a-zA-Z0-9 *] + /','',$ str);

The square brackets say select everything in this range. The carat (^) is the regex for not. We then list our valid characters (lower case a to z, uppercase a to z, numbers from 0 to 9, and an asterisks). The plus symbol on the end of the square bracket says select 0 or more characters.

方括号表示选择此范围内的所有内容。克拉(^)是不是的正则表达式。然后我们列出我们的有效字符(小写字母a到z,大写字母a到z,数字从0到9,以及星号)。方括号末尾的加号表示选择0个或更多字符。

Edit:

If this is the list of all characters you want to keep, then:

如果这是您要保留的所有字符的列表,则:

$str = preg_replace('/[^ĄąĆ毿ŹźŃńŁłÓó*]+/', '', $str);

$ str = preg_replace('/ [^ĄąĆ毿ŹźŃńŁłÓó*] + /','',$ str);

#1


4  

Make sure you use u flag for unicode while using your regex.

确保在使用正则表达式时使用u标志作为unicode。

Following works with your input:

以下处理您的输入:

$str = preg_replace('/^\W*(.*\w)\W*$/u', '$1', '~~ŚAAÓ~~' );

// str = ŚAAÓ

But this won't work: (Don't Use it)

但这不起作用:(不要使用它)

$str = preg_replace('/^\W*(.*\w)\W*$/', '$1', '~~ŚAAÓ~~' );

#2


3  

You can pass in a list of valid characters and tell the function to replace any character that is not in that list:

您可以传入有效字符列表并告诉函数替换该列表中不存在的任何字符:

$str = preg_replace('/[^a-zA-Z0-9*]+/', '', $str);

$ str = preg_replace('/ [^ a-zA-Z0-9 *] + /','',$ str);

The square brackets say select everything in this range. The carat (^) is the regex for not. We then list our valid characters (lower case a to z, uppercase a to z, numbers from 0 to 9, and an asterisks). The plus symbol on the end of the square bracket says select 0 or more characters.

方括号表示选择此范围内的所有内容。克拉(^)是不是的正则表达式。然后我们列出我们的有效字符(小写字母a到z,大写字母a到z,数字从0到9,以及星号)。方括号末尾的加号表示选择0个或更多字符。

Edit:

If this is the list of all characters you want to keep, then:

如果这是您要保留的所有字符的列表,则:

$str = preg_replace('/[^ĄąĆ毿ŹźŃńŁłÓó*]+/', '', $str);

$ str = preg_replace('/ [^ĄąĆ毿ŹźŃńŁłÓó*] + /','',$ str);