NYCPHP Meetup

NYPHP.org

[nycphp-talk] rtrim broken?

David Krings ramons at gmx.net
Wed Nov 1 07:23:12 EST 2006


Hi,

    just after I hit send it made 'ping' in my mind and I did see what 
you meant. My script is wrong.

rtrim($text, ', ');

    does trim the string alright, I just tell it to send the result to 
nowhere as I don't pick up the trimmed string. DUH! I changed it to
$text = rtrim($text, ' ,');
    and now that works. The problem was between keyboard and chair as so 
often.


          David K.


David Krings wrote:
> Hi,
>
>     yes, $text is the string passed to rtrim, which in retrun is 
> supposed to overwrite what is in $text previously. Kinda like
> $k = $k++;
> When k was 5 before this line it will be 6 after.
>
> That string in question is build by a loop, so I may take your advise 
> and do it differently. That still doesn't explain why rtrim doesn't work 
> as I expect it to.
>
>     David K.
>
> Dell Sala wrote:
>   
>> Are you reassigning the result to $text? The first argument is not  
>> passed by reference -- instead, it returns the trimmed string.
>>
>> $text = rtrim($text, ', ');
>> echo $text;
>>
>> BTW: if this string is build by a loop, there is another idiom that I  
>> find cleaner than stripping off the last separator: Build text as an  
>> array, then implode it.
>>
>> $text = array();
>> foreach (range(0, 2) as $i) {
>>     $text[] = 'blah';
>> }
>> $text = implode(', ', $text);
>>
>>
>> -- Dell
>>
>>
>> On Oct 31, 2006, at 8:33 PM, David Krings wrote:
>>
>>   
>>     
>>> Hi,
>>>
>>> I have a string that looks like this
>>> $text = 'blah, blah, blah, ';
>>> I want to use rtrim to cut off the last comma and the whitespace.  
>>> So I use
>>> rtrim($text, ', ');
>>>
>>> To my surprise, it doesn't cut anything off. When I echo $text the  
>>> comma
>>> and the space are still there.
>>>
>>> Is rtrim broken or my logic or my script?
>>>
>>> David K.
>>>
>>>       




More information about the talk mailing list