narjis Posted May 8, 2011 Share Posted May 8, 2011 Here is simple program which should return atrimmed string by using trim.I can't find why is it not working. $tr="Hira Mallick"; var_dump($tr); $new= trim($tr," lic"); var_dump ($new); Also this program is not runnig correctly $hello = "Hello World"; $trimmed = trim($hello, "HdWr"); echo($trimmed); Quote Link to comment Share on other sites More sharing options...
sunfighter Posted May 9, 2011 Share Posted May 9, 2011 You are right. You should report this. Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 10, 2011 Share Posted May 10, 2011 You didn't provide the output you are getting. Quote Link to comment Share on other sites More sharing options...
narjis Posted May 10, 2011 Author Share Posted May 10, 2011 in the first code i am getting the same string and in the second one W of World string is not trimmed off. Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 10, 2011 Share Posted May 10, 2011 You do not understand what trim does. Perhaps you should re-read the description of the function where it says: Strip whitespace (or other characters) from the beginning and end of a string Notice the emphasis I've added to 2 words in that description. Quote Link to comment Share on other sites More sharing options...
narjis Posted May 10, 2011 Author Share Posted May 10, 2011 Have you seen the example given on php manual website which is as follows: <?php $text = "\t\tThese are a few words ... "; $binary = "\x09Example string\x0A"; $hello = "Hello World"; var_dump($text, $binary, $hello); print "\n"; $trimmed = trim($text); var_dump($trimmed); $trimmed = trim($text, " \t."); var_dump($trimmed); $trimmed = trim($hello, "Hdle"); var_dump($trimmed); // trim the ASCII control characters at the beginning and end of $binary // (from 0 to 31 inclusive) $clean = trim($binary, "\x00..\x1F"); var_dump($clean); ?> The last trim gives correct result but when I change it to $trimmed = trim($hello, "HdWr"); var_dump($trimmed); it doesnot strip off W. I am attachoing the code here with. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
sunfighter Posted May 10, 2011 Share Posted May 10, 2011 You do not understand what trim does. Perhaps you should re-read the description of the function where it says: Strip whitespace (or other characters) from the beginning and end of a string Notice the emphasis I've added to 2 words in that description. It really says: This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters: The description is string trim ( string $str [, string $charlist ] ) And the explaination of the $charlist is: Optionally, the stripped characters can also be specified using the charlist parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters. So it should remove everything you add to the $charlist and it does for somethings. Very intermittent on output. Quote Link to comment Share on other sites More sharing options...
gizmola Posted May 10, 2011 Share Posted May 10, 2011 Sunfighter, if you don't have anything meaningful to add, please don't post. Not only is what you posted nonsense, it actually suggests that the function doesn't work as described which it most certainly does. It is not a search and replace function, it only removes things from the beginning and end of a string. If the characters in the list are at the beginning or end of the tring it will remove them, if not then it doesn't. This is clear from the examples. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.