From fda250ecbff3016cbc8ee042edd237c79f170977 Mon Sep 17 00:00:00 2001 From: syoustra Date: Tue, 20 Sep 2016 19:00:35 -0400 Subject: [PATCH 1/5] Initial commit of NeedlesInHaystacks Challenge --- src/string_search.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/string_search.php diff --git a/src/string_search.php b/src/string_search.php new file mode 100644 index 0000000..d7e03bd --- /dev/null +++ b/src/string_search.php @@ -0,0 +1,30 @@ + \ No newline at end of file From b8fa482f2f9cee1f945dea30178b2fbd42abfa66 Mon Sep 17 00:00:00 2001 From: syoustra Date: Tue, 20 Sep 2016 19:10:50 -0400 Subject: [PATCH 2/5] Fixing the error of non-direction-following of defining function --- src/string_search.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/string_search.php b/src/string_search.php index d7e03bd..a93f0b8 100644 --- a/src/string_search.php +++ b/src/string_search.php @@ -13,18 +13,20 @@ $haystack = 'Elephants are afraid of needles!'; $needle = 'needle'; +function string_search($needle, $haystack) { -if ($needle == '') { - echo 'false'; -} else { - $string_search = strpos($haystack, $needle); - if ($string_search !== false) { - echo "Found '$needle' at position $string_search"; + if ($needle == '') { + return false; } else { - echo 'false'; + $string_search = strpos($haystack, $needle); + if ($string_search !== false) { + return "Found '$needle' at position $string_search"; + } else { + return false; + } } } - +echo string_search(); ?> \ No newline at end of file From 63f7ba54544b74c619d4dd2d5d7ecd0a3811fe3f Mon Sep 17 00:00:00 2001 From: syoustra Date: Tue, 20 Sep 2016 19:17:56 -0400 Subject: [PATCH 3/5] Commenting out my testing info --- src/string_search.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/string_search.php b/src/string_search.php index a93f0b8..eaa27ad 100644 --- a/src/string_search.php +++ b/src/string_search.php @@ -10,8 +10,8 @@ // If needle is an empty string, string_search() should return false. // If needle is found in haystack multiple times, string_search() should return the first index. -$haystack = 'Elephants are afraid of needles!'; -$needle = 'needle'; +// $haystack = 'Elephants are afraid of needles!'; +// $needle = 'needle'; function string_search($needle, $haystack) { @@ -27,6 +27,6 @@ function string_search($needle, $haystack) { } } -echo string_search(); +// echo string_search(); ?> \ No newline at end of file From 8022421a469c757bb4d367d95d2b19f93b690ddb Mon Sep 17 00:00:00 2001 From: syoustra Date: Wed, 21 Sep 2016 00:37:38 -0400 Subject: [PATCH 4/5] Adjusting phrasing to match instruction rather than example --- src/string_search.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/string_search.php b/src/string_search.php index eaa27ad..1cfed4c 100644 --- a/src/string_search.php +++ b/src/string_search.php @@ -10,6 +10,8 @@ // If needle is an empty string, string_search() should return false. // If needle is found in haystack multiple times, string_search() should return the first index. + + // $haystack = 'Elephants are afraid of needles!'; // $needle = 'needle'; @@ -20,7 +22,7 @@ function string_search($needle, $haystack) { } else { $string_search = strpos($haystack, $needle); if ($string_search !== false) { - return "Found '$needle' at position $string_search"; + return "Found '$needle' at index $string_search"; } else { return false; } From c90fc3ec78ac3962a7d8e90b2f73cbdd2ed031e3 Mon Sep 17 00:00:00 2001 From: syoustra Date: Wed, 21 Sep 2016 00:47:35 -0400 Subject: [PATCH 5/5] Revising README phrasing to match testing expectations --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1763ead..2743e6d 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ For this PHP code exercise, create a file named `string_search.php`. This file s 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`. +For example, if *needle* were `search` and *haystack* were `string search`, `string_search()` should return `Found 'search' at index 7`. There are a few special cases: