Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/string_search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
#$haystack = 'string search';
#$needle = 'search';
function string_search($needle,$haystack){
if ($needle == ''){
return false;
}
else{
$pos = strpos($haystack, $needle);
if ($pos === false) {
return false; #return false echo "The string '$needle' was not found in the string '$haystack'";
}
else {
return "Found '$needle' at index $pos";

# return echo "The string '$needle' was found in the string '$haystack'";
# return echo " and exists at position $pos";
}
}
}
#string_search($needle,$haystack);
# String Search

#For this PHP code exercise, create a file named `string_search.php`. This file should contain
# a function named `string_search()` which accepts two parameters.
#The first is the string to search for (*needle*) and the second is the string to search
# (*haystack*).

#If *needle* is not found in *haystack*, `string_search()` should return `false`.
#If *needle* is found in *haystack*, `string_search()` should return string formatted
# as `Found 'needle' at index x` where `needle` is the first parameter and `x` is the starting index where `needle` was found.

#For example, if *needle* were `search` and *haystack* were
#`string search`, `string_search()` should return `Found 'search' at position 7`.

#There are a few special cases:

#1. If *needle* is an empty string, `string_search()` should return `false`.
#2. If *needle* is found in *haystack* multiple times, `string_search()` should return the first index.

## Just getting started?
#Review the [PHP Code Exercises](https://github.com/CodeLouisville/back-end-php/tree/master/exercises) documentation for more details on performing code exercises.

## Need help?
#Jump on the PHP channel in Slack and ask your fellow students and mentors for a hint.

?>