Reputation: 329
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"
?
Upvotes: 8
Views: 24560
Reputation:
The answer is quite simple. First you need to have a basic understanding of what the function trim() does.
The syntax of the function is:
trim(string,charlist);
The second parameter is optional. If present it removes the characters present in charlist
from both ends of the string.
Example:
$str="beautiful day";
echo trim($str, "aby");
Output:
eautiful d
The letters a
, b
and y
gets removed from both ends of the string
However, if the second parameter is omitted then the following characters are removed from both ends of the string.
"\0" - NULL
"\t" - tab
"\n" - new line
"\x0B" - vertical tab
"\r" - carriage return
" " - ordinary white space
In your case the output is:
string(11) "Single Tile" string(6) "Author" string(21) "Single Tile_by_Author"
The space between Single
and Tile_by_Author
remains as white spaces are removed from both ends only
Upvotes: 0
Reputation: 912
array('username','filter','filter'=>'trim')
array('templatename','filter','filter'=>'trim')
Add above lines in rules
Upvotes: 1
Reputation: 167162
The function trim()
removes only the spaces (and control characters such as \n) from the beginning and the end of the string.
$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()
.
Upvotes: 11
Reputation: 22711
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.
$title = str_replace(' ', '', $title);
Syntax: str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
search
The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
replace The replacement value that replaces found search values. An array may be used to designate multiple replacements.
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.
To more about : https://www.php.net/str_replace
Upvotes: 0
Reputation: 2462
Trim is for edges.
Just use:
$title = str_replace(' ', '', $title);
$user = str_replace(' ', '', $user);;
Upvotes: 0
Reputation: 5744
Let's replace not only space but all ASCII Controll Chars.
ex. Space
Tab
End of Line
$str = preg_replace("/[\\x0-\x20\x7f]/", '', $str);
Upvotes: 1
Reputation: 172
http://www.php.net/manual/en/function.trim.php trim — Strip whitespace (or other characters) from the beginning and end of a string
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:
$title = str_replace(' ','',$title);
Upvotes: 0
Reputation: 954
Trim only removes spaces at the beginning and the end, not all spaces.
str_replace(' ','',$string);
to remove all spaces
Upvotes: 0