Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Question on strings in PHP

Author
Message
Mistrel
Retired Moderator
19
Years of Service
User Offline
Joined: 9th Nov 2005
Location:
Posted: 4th Nov 2006 04:27
Would anyone here be able to help me with a problem? I'm learning PHP and I don't know how to trim a single character off the end of a string.

http://3dfolio.com
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 4th Nov 2006 04:50 Edited at: 4th Nov 2006 04:55
similar to string length in db you can use the strlen() function to find the length.

from what i understand there is an explode function as well



and looking again there is a strip function that might help

perhaps getting the length and reading the ascii back in with a loop minus one would do the trick

I found all this looking on the net at the oz php site.


http://au.php.net/strlen

Mistrel
Retired Moderator
19
Years of Service
User Offline
Joined: 9th Nov 2005
Location:
Posted: 4th Nov 2006 06:43
I found a function called rtrim(). This handled it just as well. Thanks.

http://3dfolio.com
indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 4th Nov 2006 07:21
good work.

QuothTheRaven
22
Years of Service
User Offline
Joined: 2nd Oct 2002
Location: United States
Posted: 4th Nov 2006 09:11
From php.net:

Quote: "string rtrim ( string str [, string charlist] )
This function returns a string with whitespace stripped from the end of str. Without the second parameter, rtrim() will strip these characters:
" " (ASCII 32 (0x20)), an ordinary space.
"\t" (ASCII 9 (0x09)), a tab.
"\n" (ASCII 10 (0x0A)), a new line (line feed).
"\r" (ASCII 13 (0x0D)), a carriage return.
"\0" (ASCII 0 (0x00)), the NUL-byte.
"\x0B" (ASCII 11 (0x0B)), a vertical tab."

This does not sound like what you want to do. If you don't know what the character at the end of the string is going to be you'd need to make up a bulky delimiter to catch all possible characters. I think substr() would better suit your needs:

http://us3.php.net/manual/en/function.substr.php
Quote: "$rest = substr("abcdef", 0, -1); // returns "abcde""

just use -1 as the second paramater and it will chop off the last character of any string you give it.

QuothTheRaven
22
Years of Service
User Offline
Joined: 2nd Oct 2002
Location: United States
Posted: 4th Nov 2006 09:13
Ok, after trying to edit that last post (sorry if i'm derailing anything), just testing out something:
"\0" Will this text show up if I try to edit the message?

Richard Davey
Retired Moderator
22
Years of Service
User Offline
Joined: 30th Apr 2002
Location: On the Jupiter Probe
Posted: 4th Nov 2006 12:43
Quote: "I'm learning PHP and I don't know how to trim a single character off the end of a string."


The easiest way? Like this:

$string = substr($string, 0, -1);

"Bite my shiny metal ass" (Futurama)
"Don't ping my cheese with your bandwidth" (Dilbert)

Login to post a reply

Server time is: 2024-11-17 19:26:06
Your offset time is: 2024-11-17 19:26:06