By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,760 Members | 1,630 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,760 IT Pros & Developers. It's quick & easy.

rightmost characters

P: n/a
Does PHP provide a function to extract the rightmost characters
from a string?

For example, the VB/ASP equivalent...

sStateAbbr = Right$(sCityState, 2)

Thanks.

Jim Carlock
Post replies to the group.
Feb 20 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Jim Carlock" <an*******@127.0.0.1> wrote in message
news:IR*******************@tornado.tampabay.rr.com ...
Does PHP provide a function to extract the rightmost characters
from a string?

For example, the VB/ASP equivalent...

sStateAbbr = Right$(sCityState, 2)

Thanks.

Jim Carlock
Post replies to the group.


I think there is a function, but offhand can't recall it and a quick search
of www.php.net didn't reveal it quickly. However, there is always:

$sStateAbbr = substr($sCityState, strlen($sCityState) - 2);

Remember that PHP variables start with a dollar sign.

Shelly
Feb 20 '06 #2

P: n/a
On Mon, 20 Feb 2006 01:22:48 +0000, Jim Carlock wrote:
Does PHP provide a function to extract the rightmost characters from a
string?

For example, the VB/ASP equivalent...

sStateAbbr = Right$(sCityState, 2)


You should pay more detailed attention to the manual. Standard PHP
"substr" function does that, without any additional functions. Here is
what the fine manual says:

Description
string substr ( string string, int start [, int length] )

substr() returns the portion of string specified by the start and length
parameters.

If start is non-negative, the returned string will start at the start'th
position in string, counting from zero. For instance, in the string
'abcdef', the character at position 0 is 'a', the character at position 2
is 'c', and so forth.

Example 1. Basic substr() usage
<?php
echo substr('abcdef', 1); // bcdef echo substr('abcdef', 1, 3); // bcd
echo substr('abcdef', 0, 4); // abcd echo substr('abcdef', 0, 8); //
abcdef echo substr('abcdef', -1, 1); // f

// Accessing single characters in a string // can also be achived using
"curly braces" $string = 'abcdef';
echo $string{0}; // a
echo $string{3}; // d
echo $string{strlen($string)-1}; // f

?>

If start is negative, the returned string will start at the start'th
character from the end of string.

Here is an example:
#!/usr/local/bin/php
<?php
$a="Usenet is a great tool";
echo substr($a,-4),"\n";
?>

When executed, the output given is "tool".
--
http://www.mgogala.com

Feb 20 '06 #3

P: n/a
Jim Carlock wrote:
Does PHP provide a function to extract the rightmost characters
from a string?

For example, the VB/ASP equivalent...

sStateAbbr = Right$(sCityState, 2)

Thanks.

Jim Carlock
Post replies to the group.


From the PHP manual/String Functions/substr (start at
http://php.benscom.com/manual/en/). Note paragraph highlighted with
asterisks.

substr

(PHP 3, PHP 4, PHP 5)
substr -- Return part of a string
Description
string substr ( string string, int start [, int length] )

substr() returns the portion of string specified by the start and length
parameters.

If start is non-negative, the returned string will start at the start'th
position in string, counting from zero. For instance, in the string
'abcdef', the character at position 0 is 'a', the character at position
2 is 'c', and so forth.

************************************************** ******************
If start is negative, the returned string will start at the start'th
character from the end of string.
************************************************** ******************

If length is given and is positive, the string returned will contain
at most length characters beginning from start (depending on the length
of string). If string is less than or equal to start characters long,
FALSE will be returned.

If length is given and is negative, then that many characters will be
omitted from the end of string (after the start position has been
calculated when a start is negative). If start denotes a position beyond
this truncation, an empty string will be returned.

Feb 20 '06 #4

P: n/a
"Jim Carlock" previously asked:
Does PHP provide a function to extract the rightmost characters
from a string?
On Sun, 19 Feb 2006 21:32:19 -0500
"Shelly" <sh************@asap-consult.com> posted:

"Shelly" <sh************@asap-consult.com> commented: I think there is a function, but offhand can't recall it and a quick
search of www.php.net didn't reveal it quickly. However,
there is always:

$sStateAbbr = substr($sCityState, strlen($sCityState) - 2);


Hi, Shelly,

Thank you for your post. The function to use is substr(). If you
insert a negative number as the second parameter, it returns the
requested number of rightmost characters. I ran into the same
problem you ran into regarding the PHP.NET site, whereby
searching for "right characters" just fails to yield anything
quickly.

I had already found the answer when I posted. The reason
for asking was two fold. (1) To see what kind of other answers
might turn up. (2) "The power of suggestion."

The second goal provides a hint to others.

Rather than allowing everyone to guess at what I'm suggesting,
I'm going to lay it out straight and true...

The following keywords would be helpful for finding the answer
to the question asked, in other words, the keywords should be
applied to the bottom of the page and to the META keyword list.

right$, rightmost, right most characters, right characters, trailing
character extraction, final character extraction

Maybe someone at php.net will read and give some consideration to
the idea. If anyone else reads this and feels its a worthwhile
investment, or if they know how to contact anyone that works for
the organization, feel free to pass the idea on. I'd be willing to
invest an hour a night into adding keywords. Let the folks at
PHP.NET know.

And giving this some thought, for the following function,
is there another way to write it or another suggestion?

function ExtractConditionCategory() {
if (isset($_GET['switch'])) {
return('switch');
} elseif (isset($_GET['if'])) {
return('if');
} elseif (isset($_GET['while'])) {
return('while');
} elseif (isset($_GET['for'])) {
return('for');
} elseif (isset($_GET['foreach'])) {
return('foreach');
} else {
// default to do-while
return('do-while');
}
}

Thanks much.

Jim Carlock
Post replies to the newsgroup.
Feb 22 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.