php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42052 Trim doesn't trim correctly with null as charlist
Submitted: 2007-07-20 13:27 UTC Modified: 2007-07-21 00:22 UTC
From: martin at martinmoen dot com Assigned:
Status: Wont fix Package: Strings related
PHP Version: 5.2.3 OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
18 - 10 = ?
Subscribe to this entry?

 
 [2007-07-20 13:27 UTC] martin at martinmoen dot com
Description:
------------
The trim function doesn't trim away whitespaces when giving it null value in the charlist.

Reproduce code:
---------------
function array_trim($arr, $charlist=null)
{
	if (is_array($arr))
	{
		foreach($arr as $key => $value)
		{
			if (is_array($value)) $result[$key] = array_trim($value, $charlist);
			else $result[$key] = trim($value, $charlist);
		}
		return $result; 
	}
}
var_dump(array_trim(array(' foo', 'bar ')));

Expected result:
----------------
array(2) {
  [0]=>
  string(3) "foo"
  [1]=>
  string(3) "bar"
}

Actual result:
--------------
array(2) {
  [0]=>
  string(4) " foo"
  [1]=>
  string(4) "bar "
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-20 16:40 UTC] jani@php.net
If I understood correctly, you want to be able to do 'trim($foo)' but by passing 'trim($foo, null)'? 
 [2007-07-20 21:39 UTC] jani@php.net
In any case, we're not changing the behaviour at this point. 
The function is clearly documented expecting a string as the charlist parameter. 
 [2007-07-21 00:22 UTC] martin at martinmoen dot com
To your earlier reply, yes, I excpected trim to use the standard charlist when passed with the value null as charlist.

I think at least there should be some kind of warning or notice that trim will strip away nothing when passed with the charlist=null
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 23:01:29 2024 UTC