473,830 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

rightmost characters

Does PHP provide a function to extract the rightmost characters
from a string?

For example, the VB/ASP equivalent...

sStateAbbr = Right$(sCitySta te, 2)

Thanks.

Jim Carlock
Post replies to the group.
Feb 20 '06 #1
4 9260

"Jim Carlock" <an*******@127. 0.0.1> wrote in message
news:IR******** ***********@tor nado.tampabay.r r.com...
Does PHP provide a function to extract the rightmost characters
from a string?

For example, the VB/ASP equivalent...

sStateAbbr = Right$(sCitySta te, 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($sCitySt ate, strlen($sCitySt ate) - 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$(sCitySta te, 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$(sCitySta te, 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($sCitySt ate, strlen($sCitySt ate) - 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 ExtractConditio nCategory() {
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
47827
by: Pikkel | last post by:
i'm looking for a way to replace special characters with characters without accents, cedilles, etc.
7
9010
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, mainly because I need the ability to both create and extract zip-files. I've tried a couple of classes found here and there, and they all seem to have the same problem. I'm currently using PclZip (http://phpconcept.net/pclzip/) but even the...
0
519
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 browser window border). How do I code this in HTML resp. CSS? If the user shrinks the browser window to a size which is smaller than the sum of the size of the two images image1.jpg + image2.jpg then all browser tend to position these pictures...
43
3798
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 in a string is defined by a Unicode scalar value, also called ...
4
3859
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 characters"(No. 128 to 255) . e.g., in one version No. 224 is the symbol alpha, in another, it's a "a" with a ` on it... How come? You can see it here: http://www.kturby.com/cables/ascii2.htm
6
1516
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 microsoft.visualbasic.right(mystring, 2) = ??????? Then
11
11194
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 a text box. I know that there are certain control characters embedded in the data that I want to display substitutions for in the text box, for instance I want to replace ASCII character 04 with the string "<EOT>". I have tried doing a simple...
0
1185
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, and click on one of the rightmost cells the form scrolls to the leftmost end of the datagridview. I tried various permutations of the form's autoscroll, the datagridview's scrollbars, and the datagridview's selection mode without success. What do...
3
10205
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. This is not a regular expression tutorial. Assumes you are already familiar with basic regular expression concepts and terminology. If not, you may want to read some regular expression tutorial. See the end of the article for links to online resources....
0
9793
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10774
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10526
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10206
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7746
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3076
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.