0

I've been trying to search for a way to find a string within another string and return the position where it was found using PHP, but not using strpos as this will help me learn programming without relying on native functions so much.

How would I go about this?

8
  • 1
    Native functions are made for us to rely on. Commented Jul 12, 2016 at 22:22
  • 2
    But let's give it a try. What have you done so far? You said that you don't want to rely on native functions (but you are now relying on 'forums'). So you should be able to come up with code of your own. Practice logic ;) Commented Jul 12, 2016 at 22:23
  • It's not, if you could just point me in documentation, I'd be more than happy to research it myself bro? Commented Jul 12, 2016 at 22:23
  • 2
    Well I'm guessing, iterating through a for loop where $a = 'cookies' and $b = 'coo' and then maybe checking if the value of $b's index position aka c is equal to $a's c? I'm just super confused kinda Commented Jul 12, 2016 at 22:25
  • @Steven Your guess is right. Now you can just try to code it. Commented Jul 12, 2016 at 22:28

2 Answers 2

2

I don't think there's anything wrong with relying on native functions, but if you are just trying to figure out how something like strpos could work behind the scenes, here is a very basic example that only uses language constructs:

function my_strpos ($haystack, $needle, $offset = 0) {

    // for loop with indexes for both strings, up to the length of $haystack
    for ($h_index=$offset, $n_index=0; isset($haystack[$h_index]); $h_index++, $n_index++) {

        if ($haystack[$h_index] == $needle[$n_index]) {           // *CHARACTERS MATCH*

            if (!isset($start_pos)) $start_pos = $h_index;        // set start_pos if not set

            if (!isset($needle[$n_index + 1])) return $start_pos; // all characters match


        } else {                                                  // *CHARACTERS DON'T MATCH*

            $n_index = -1;                                        // reset $needle index

            unset($start_pos);                                    // unset match start pos.
        }
    }
    // all charactes of $haystack iterated without matching $needle
    return false;
}

This is obviously a naive implementation which doesn't check for valid input or handle errors, but hopefully it will demonstrate one possible method of finding one string within another.

If you really want to know how strpos does work behind the scenes, here is a great (although a bit dated) article about how to understand the PHP source, that happens to use strpos as an example.

Sign up to request clarification or add additional context in comments.

Comments

0

You can use strstr, documentation here

<?php 
   $haystack = "Hello my dear friend";
   $needle = "friend";
// Returns false if needle is not inside haystack
   strstr($haystack, $needle);
?>

1 Comment

@TimMalone I kinda hope you are being sarcastic. It was probably done without reading the question (that talks about native functions). Besides, this code doesn't even 'find' the position of a string inside another: https://eval.in/604353 - no position returned...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.