Home > Backend Development > PHP Tutorial > Which PHP function can remove all characters on both sides of text? (all)

Which PHP function can remove all characters on both sides of text? (all)

WBOY
Release: 2023-03-02 08:32:01
Original
1244 people have browsed it

trim removes whitespace characters or other predefined characters on both sides of a string.
If you specify the second parameter (the symbol that needs to be removed), can you remove more than one?
But now I am not sure what kind of symbols I will enter, such as question mark, dot, comma, and exclamation mark. It is impossible to type all the symbols, right?
So, is there any function that can handle all the symbols on both sides of the string?

Reply content:

trim removes whitespace characters or other predefined characters on both sides of a string.
If you specify the second parameter (the symbol that needs to be removed), can you remove more than one?
But now I am not sure what kind of symbols I will enter, such as question mark, dot, comma, and exclamation mark. It is impossible to type all the symbols, right?
So, is there any function that can handle all the symbols on both sides of the string?

You can use preg_replace to replace unnecessary characters with empty characters.

<code>echo preg_replace('/[!:@#]/', '', '@thi:s! test#');

###输出
this test</code>
Copy after login

Not only the beginning and the end, but also all positions will be deleted. If necessary, the rules need to be changed.

Can I use regular expressions to match the first and last characters and parameterize the matching content and times?

<code>1.正则匹配处理
2.生成这个字符串的时候就不要这些字符</code>
Copy after login

Using regular expressions is the best way, take a closer look, w can match all character characters, thereby excluding special symbols

You can change your mind and limit the input type to only allow numbers, letters and underscores to be entered

<code class="code"><?
        preg_match('/(\w+){6,12}/',$input_string,$matches);
</code>
Copy after login

Since you are not sure about the user input, what do you need to remove. Then what you definitely want is definitely certain, so don’t use the blacklist system. It’s easier to consider the whitelist.

That is, you are allowed to enter whatever you want, and everything else will not pass~

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template