Skip to content

Commit 98d61e8

Browse files
author
Martin Brecht-Precht
committed
Fixed an issue with the lastIndexOf method.
Updated readme.
1 parent 700f784 commit 98d61e8

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed

README.md

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,45 +29,83 @@ A basic string builder library providing different string methods written in PHP
2929
require_once('path/to/vendor/autoload.php');
3030
```
3131

32-
### Building a string
32+
### Building and modifying a string
3333

3434
```{php}
3535
use Markenwerk\StringBuilder\StringBuilder;
3636
37-
$builder = new StringBuilder();
37+
$builder = new StringBuilder('rolod muspi meroL');
3838
$builder
39-
->append('a')
39+
->reverse()
40+
->append(' sit amet, consetetur')
4041
->append(12)
4142
->append(false)
4243
->prepend('b')
43-
->remove(1)
44-
->replace(0, 'ab')
45-
->append(true);
44+
->append(true)
45+
->insert(1, 'qäs')
46+
->replace(6, 2, 'wertz')
47+
->setCharAt(4, '2')
48+
->delete(0, 2)
49+
->delete(40)
50+
->deleteCharAt(3);
4651
47-
$string = $builder->build();
48-
fwrite(STDOUT, 'Result "' . $string . '"' . PHP_EOL);
52+
$result = $builder->build();
53+
fwrite(STDOUT, ' 1. Built string ' . $result . PHP_EOL);
4954
50-
$substring = $builder->buildSubstring(0, 2);
51-
fwrite(STDOUT, 'Substring result from position 0 and size 2 "' . $substring . '"' . PHP_EOL);
55+
$result = $builder->buildSubstring(5, 2);
56+
fwrite(STDOUT, ' 2. Built substring ' . $result . PHP_EOL);
5257
53-
$substring = $builder->buildSubstring(1);
54-
fwrite(STDOUT, 'Substring result from position 1 till the end "' . $substring . '"' . PHP_EOL);
58+
$result = $builder->buildSubstring(5);
59+
fwrite(STDOUT, ' 3. Built substring ' . $result . PHP_EOL);
5560
56-
$size = $builder->size();
57-
fwrite(STDOUT, 'Builder holds "' . $size . '" partial strings' . PHP_EOL);
61+
$result = $builder->charAt(5);
62+
fwrite(STDOUT, ' 4. Character ' . $result . PHP_EOL);
63+
```
64+
65+
will output the following
66+
67+
```{http}
68+
1. Built string ä2wertzem ipsum dolor sit amet, conset
69+
2. Built substring rt
70+
3. Built substring rtzem ipsum dolor sit amet, conset
71+
4. Character r
72+
```
73+
74+
### Getting string properties
75+
76+
```{php}
77+
$result = $builder->length();
78+
fwrite(STDOUT, ' 5. String length ' . $result . PHP_EOL);
79+
80+
$result = $builder->size();
81+
fwrite(STDOUT, ' 6. Number of bytes ' . $result . PHP_EOL);
82+
83+
$result = $builder->indexOf('e');
84+
fwrite(STDOUT, ' 7. First occurence of \'e\' ' . $result . PHP_EOL);
85+
86+
$result = $builder->indexOf('e', 5);
87+
fwrite(STDOUT, ' 8. First occurence of \'e\' after position 5 ' . $result . PHP_EOL);
88+
89+
$result = $builder->lastIndexOf('e');
90+
fwrite(STDOUT, ' 9. Last occurence of \'e\' ' . $result . PHP_EOL);
91+
92+
$result = $builder->lastIndexOf('e', 5);
93+
fwrite(STDOUT, '10. Last occurence of \'e\' before the 5th last character ' . $result . PHP_EOL);
5894
59-
$length = $builder->length();
60-
fwrite(STDOUT, 'Resulting string length is "' . $length . '" characters' . PHP_EOL);
95+
$result = $builder->contains('ipsum');
96+
fwrite(STDOUT, '12. Whether the string contains \'ipsum\' ' . $result . PHP_EOL);
6197
```
6298

6399
will output the following
64100

65101
```{http}
66-
Result "ab121"
67-
Substring result from position 0 and size 2 "ab12"
68-
Substring result from position 1 till the end "121"
69-
Builder holds "4" partial strings
70-
Resulting string length is "5" characters
102+
5. String length 38
103+
6. Number of bytes 39
104+
7. First occurence of 'e' 4
105+
8. First occurence of 'e' after position 5 8
106+
9. Last occurence of 'e' 37
107+
10. Last occurence of 'e' before the 5th last character 29
108+
12. Whether the string contains 'ipsum' 1
71109
```
72110

73111
---

src/StringBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public function lastIndexOf($string, $offset = 0)
207207
->validateScalar($string)
208208
->validateEmpty($string)
209209
->validateInteger($offset);
210-
$index = mb_strrpos($this->string, (string)$string, $offset);
210+
$index = mb_strrpos($this->string, (string)$string, -1 * $offset);
211211
return $index === false ? null : $index;
212212
}
213213

0 commit comments

Comments
 (0)