Comments (3)
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.
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.
Thank you for the explanation.
from string.
Related Issues (11)
- Sample code for replaceFirst are not match HOT 1
- startsWith() and endsWith() methods HOT 2
- [Feature Request] Callback on the result of the explode HOT 1
- feature request: fluent
- Package anahkiasen/underscore-php is abandoned, you should avoid using it. No replacement was suggested. HOT 2
- Package anahkiasen/underscore-php is abandoned HOT 1
- Anahkiasen/underscore-php Deprecated HOT 2
- Is this typo? HOT 5
- Laravel 9 Compatibility HOT 1
- replaceFirst
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from string.