rightmost characters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Carlock

    #1

    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.


  • Shelly

    #2
    Re: rightmost characters


    "Jim Carlock" <anonymous@127. 0.0.1> wrote in message
    news:IR8Kf.3268 7$Fw6.15304@tor nado.tampabay.r r.com...[color=blue]
    > 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.
    >
    >[/color]

    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


    Comment

    • Mladen Gogala

      #3
      Re: rightmost characters

      On Mon, 20 Feb 2006 01:22:48 +0000, Jim Carlock wrote:
      [color=blue]
      > Does PHP provide a function to extract the rightmost characters from a
      > string?
      >
      > For example, the VB/ASP equivalent...
      >
      > sStateAbbr = Right$(sCitySta te, 2)[/color]

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


      --
      大红鹰娱乐平台采用顶级加密技术,确保用户数据与交易的安全无虞,让玩家能够放心畅玩。


      Comment

      • Bob Stearns

        #4
        Re: rightmost characters

        Jim Carlock wrote:[color=blue]
        > 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.
        >
        >[/color]

        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.

        Comment

        • Jim Carlock

          #5
          Re: rightmost characters

          "Jim Carlock" previously asked:[color=blue]
          > Does PHP provide a function to extract the rightmost characters
          > from a string?[/color]

          On Sun, 19 Feb 2006 21:32:19 -0500
          "Shelly" <sheldonlg.news @asap-consult.com> posted:

          "Shelly" <sheldonlg.news @asap-consult.com> commented:[color=blue]
          > 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);[/color]

          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.

          Comment

          Working...