Question

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"?

Was it helpful?

Solution

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().

OTHER TIPS

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

ex. Space Tab End of Line

$str = preg_replace("/[\\x0-\x20\x7f]/", '', $str);
 array('username','filter','filter'=>'trim')
array('templatename','filter','filter'=>'trim')

Add above lines in rules

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

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

to remove all spaces

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);

Trim is for edges.

Just use:

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

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 : http://in3.php.net/str_replace

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top