PHP 函数 trim() 不会从字符串中间剥离空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21218975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:55:00  来源:igfitidea点击:

PHP function trim() not stripping whitespace from the middle of the string

phpstringtrim

提问by user3195417

I have some pretty straight-forward code, but something's going wrong. The following code

我有一些非常直接的代码,但是出了点问题。以下代码

$title = $_POST['templatename'];
$user = $_POST['username'];
$selectedcoordinates = $_POST['templatestring'];

$title = trim($title);
$user = trim($user);


$filename = $title . "_by_" . $user;

var_dump($title);
var_dump($user);
var_dump($filename);

returns this:

返回这个:

string(11) "Single Tile"
string(6) "Author"
string(21) "Single Tile_by_Author" 

where the values originate from a HTML form. Why doesn't "Single Tile"become "SingleTile"?

其中值来自 HTML 表单。为什么不"Single Tile"变成"SingleTile"

回答by Praveen Kumar Purushothaman

The function trim()removes only the spaces (and control characters such as \n) from the beginning and the end of the string.

该函数trim()仅从字符串的开头和结尾删除空格(和控制字符,例如 \n)。

$title = str_replace(" ", "", trim($title));
$user = str_replace(" ", "", trim($user));

I just wanted to attack the problem. So, the solution may look bad. Anyways, the best use for this is by using this way:

我只是想解决这个问题。因此,该解决方案可能看起来很糟糕。无论如何,最好的用途是使用这种方式:

$title = str_replace(" ", "", $title);
$user = str_replace(" ", "", $user);

I removed the trim()function because str_replace()does the job of trim().

我删除的trim()功能,因为str_replace()做的工作trim()

回答by Samar Haider

 array('username','filter','filter'=>'trim')
array('templatename','filter','filter'=>'trim')

Add above lines in rules

在规则中添加以上几行

回答by mpyw

Let's replace not only space but all ASCII Controll Chars.

让我们不仅替换空格,还替换所有ASCII Controll Chars

ex. SpaceTabEnd of Line

前任。 SpaceTabEnd of Line

$str = preg_replace("/[\x0-\x20\x7f]/", '', $str);

回答by Krish R

You can use str_replace()function, This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.

您可以使用str_replace()函数,该函数返回一个字符串或数组,其中所有出现的主题中的搜索都被给定的替换值替换。

  $title = str_replace(' ', '', $title);

Syntax: str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

语法:str_replace (混合 $search ,混合 $replace ,混合 $subject [, int &$count ] )

search

搜索

The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.

正在搜索的值,也称为针。阵列可用于指定多个针。

replaceThe replacement value that replaces found search values. An array may be used to designate multiple replacements.

replace替换找到的搜索值的替换值。数组可用于指定多个替换。

subject

主题

The string or array being searched and replaced on, otherwise known as the haystack.

被搜索和替换的字符串或数组,也称为干草堆。

If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well.

如果subject是一个数组,则对subject的每一个条目进行查找和替换,返回值也是一个数组。

To more about : http://in3.php.net/str_replace

更多信息:http: //in3.php.net/str_replace

回答by Saurabh Sharma

Trim is for edges.

Trim 用于边缘。

Just use:

只需使用:

$title = str_replace(' ', '', $title);
$user = str_replace(' ', '', $user);;

回答by Tobias

Trim only removes spaces at the beginning and the end, not all spaces.

Trim 仅删除开头和结尾的空格,而不是所有空格。

str_replace(' ','',$string);

to remove all spaces

删除所有空格

回答by JaGo

http://www.php.net/manual/en/function.trim.phptrim — Strip whitespace (or other characters) from the beginning and end of a string

http://www.php.net/manual/en/function.trim.phptrim — 从字符串的开头和结尾去除空格(或其他字符)

This means trim will only delete whitespaces at the end and at the ebginning of a string, not inside your string. You may try this:

这意味着trim 只会删除字符串末尾和结尾处的空格,而不是字符串内部的空格。你可以试试这个:

$title = str_replace(' ','',$title);

回答by JaGo

The answer is quite simple. First you need to have a basic understanding of what the function trim() does.

答案很简单。首先,您需要对函数trim() 的作用有一个基本的了解。

The syntax of the function is:

该函数的语法是:

trim(string,charlist);

修剪(字符串,字符列表);

The second parameter is optional. If present it removes the characters present in charlistfrom both endsof the string.

第二个参数是可选的。如果存在,它将charlist从字符串的两端删除存在的字符。

Example:

例子:

$str="beautiful day";
echo trim($str, "aby");

Output:

输出:

eautiful d

The letters a, band ygets removed from both ends of the string

字母a,by从字符串的两端删除

However, if the second parameter is omitted then the following characters are removed from both endsof the string.

但是,如果省略第二个参数,则会从字符串的两端删除以下字符。

"
string(11) "Single Tile" string(6) "Author" string(21) "Single Tile_by_Author" 
" - NULL "\t" - tab "\n" - new line "\x0B" - vertical tab "\r" - carriage return " " - ordinary white space

In your case the output is:

在你的情况下,输出是:

##代码##

The space between Singleand Tile_by_Authorremains as white spaces are removed from both endsonly

之间的空间SingleTile_by_Author遗体的白色空间从两个删除两端