Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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:
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 "".


I think substr_replace should be okay, I'll do some tests. Thanks

Quote:
Are you embedding your PHP in a web page, or using it for heavy processing tasks?


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:
  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.


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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho