473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

rightmost characters

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
4 9244

"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Pikkel | last post by:
i'm looking for a way to replace special characters with characters without accents, cedilles, etc.
7
by: Roy W. Andersen | last post by:
I've been searching google about this for days but can't find anything, so I'm hoping someone here can help me out. I'm trying to create zip-files without needing the zip-file extension in PHP,...
0
by: Jason Stacy | last post by:
On a webpage I would like to move image1.jpg as left as possible (directly to the browser window left border) and - simultaneously - the picture image2.jpg to the rightmost (directly to the right...
43
by: Vladimir | last post by:
Method UnicodeEncoding.GetMaxByteCount(charCount) returns charCount * 2. Method UTF8Encoding.GetMaxByteCount(charCount) returns charCount * 4. But why that? Look: /* Each Unicode character...
4
by: wob | last post by:
Many thanks for those who responded to my question of "putting greek char into C string". In searching for an solution, I noticed that there are more than one version of "Extended ASCII...
6
by: Dave | last post by:
How can I determine if the two rightmost elements of my string are ." ? Obviously, vb.net has a special use for the double-quote character, so how do I account for that. I have... if...
11
by: Ron L | last post by:
I have a barcode scanner which uses a "keyboard wedge" program so that the data it scans comes through as if it was typed on a keyboard. I am trying to have the data in the barcode be displayed in...
0
by: Christopher Lusardi | last post by:
Hello, I have a datagridview that is too wide for the form, so I'm trying to put scrollbars on the form and the datagridview table. But, when I run the application, look at the rightmost cells,...
3
KevinADC
by: KevinADC | last post by:
Purpose The purpose of this article is to discuss the difference between characters inside a character class and outside a character class and some special characters inside a character class....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.