| |||||||||||
| |||||||||||
Removing last character of a string Posted by pgroves (pgroves), 24 October 2002 Is there a function to remove the last character (not whitespace, for example a surplus comma) of a string?If not, what's the most efficient way of doing this with a very long string? cheers Paul Posted by admin (Graham Ellis), 25 October 2002 Do NOT use the chop function to try to do this - in Perl, chop removes the last character in a string, but in PHP it remomes trailing white spaces ...substr_replace can be used to replace part of one string with another, and written: $outgoing = substr_replace($incoming,"",-1); it will replace the last character of a string with nothing. As regards efficiency ... I'm not sure. I feel that this will be much better than cranking up a regular expression engine to do the job (although the code would be just as short), but I am concerned at string duplication. You could experiment with handling a string as an array, and replacing the last element of that array with "". Are you embedding your PHP in a web page, or using it for heavy processing tasks? I just ask because your own efficiency, and the efficiency that any maintainer can later achieve, might be far more important than saving a few machine cycles. If the output result of your task is web page(s), then the amount of data to be presented on each page will be somewhat limited - I know web pages are getting bigger but ... Posted by pgroves (pgroves), 25 October 2002 on 10/25/02 at 07:26:51, Graham Ellis wrote:
I think substr_replace should be okay, I'll do some tests. Thanks Quote:
This particular program I'm working on is for processing a large text file, analysing (its not just csv) and splitting it up, then putting the various bits into different tables a database (this bit I've figured out how to do reasonably efficiently). Its not for web output really (though I want to be able to run it from the web). I guess efficiency isn't that important, as its only something that will be run occassionally by admin users, but I want to get into the practice of writing efficient code for the job. Quote:
Sure, good point cheers Paul This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |