Skip to content

Commit 24b6349

Browse files
authored
feat: add php solution to lc problem: No.0028 (doocs#912)
No.0028.Find the Index of the First Occurrence in a String
1 parent e63e97c commit 24b6349

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

Diff for: solution/0000-0099/0028.Find the Index of the First Occurrence in a String/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,31 @@ impl Solution {
345345
}
346346
```
347347

348+
### **PHP**
349+
350+
```php
351+
class Solution {
352+
/**
353+
* @param String $haystack
354+
* @param String $needle
355+
* @return Integer
356+
*/
357+
function strStr($haystack, $needle) {
358+
$strNew = str_replace($needle, "+", $haystack);
359+
$cnt = substr_count($strNew, "+");
360+
if ($cnt > 0) {
361+
for ($i = 0; $i < strlen($strNew); $i++) {
362+
if ($strNew[$i] == "+") {
363+
return $i;
364+
}
365+
}
366+
} else {
367+
return -1;
368+
}
369+
}
370+
}
371+
```
372+
348373
### **...**
349374

350375
```

Diff for: solution/0000-0099/0028.Find the Index of the First Occurrence in a String/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,31 @@ impl Solution {
331331
}
332332
```
333333

334+
### **PHP**
335+
336+
```php
337+
class Solution {
338+
/**
339+
* @param String $haystack
340+
* @param String $needle
341+
* @return Integer
342+
*/
343+
function strStr($haystack, $needle) {
344+
$strNew = str_replace($needle, "+", $haystack);
345+
$cnt = substr_count($strNew, "+");
346+
if ($cnt > 0) {
347+
for ($i = 0; $i < strlen($strNew); $i++) {
348+
if ($strNew[$i] == "+") {
349+
return $i;
350+
}
351+
}
352+
} else {
353+
return -1;
354+
}
355+
}
356+
}
357+
```
358+
334359
### **...**
335360

336361
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
/**
3+
* @param String $haystack
4+
* @param String $needle
5+
* @return Integer
6+
*/
7+
function strStr($haystack, $needle) {
8+
$strNew = str_replace($needle, "+", $haystack);
9+
$cnt = substr_count($strNew, "+");
10+
if ($cnt > 0) {
11+
for ($i = 0; $i < strlen($strNew); $i++) {
12+
if ($strNew[$i] == "+") {
13+
return $i;
14+
}
15+
}
16+
} else {
17+
return -1;
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)