Git Product home page Git Product logo

Comments (3)

manamski avatar manamski commented on May 23, 2024

I think I know myself.
Because there is no value "-0" (for the last element) and string('foo/bar/baz')->segment('/', -1); return before last element "bar" not "baz".
So, why not create a new method "segmentRev($delimiter, $index)", but with positive index counting from the last element?
0 = the last element ("baz")
1 = before last element ("bar")
2 = previous element ("foo")

Edit:
I looked at the source code of the method "lastSegment":

    public function lastSegment($delimiter)
    {
        return (new static($this->string))->segment($delimiter, -1);
    }

...and found that it uses the index with a value "-1". Well, now I don't understand.

from string.

sebastiandedeyne avatar sebastiandedeyne commented on May 23, 2024

The example with 3 parameters is a mistake, probably a remnant of an older version of the package.

The correct usage is:

string('foo/bar/baz')->segment('/', 0); // 0 returns the first segment
string('foo/bar/baz')->segment('/', 1); // 1 returns the second segment
string('foo/bar/baz')->segment('/', -1); // -1 returns the last segment

(if I'd write this again, I'd probably use a 1-based index, and disallow querying for the 0 segment)

from string.

manamski avatar manamski commented on May 23, 2024

Thank you for the explanation.

from string.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.