Logo
/
Programming
/
PHP Course
/

Finding a starting position

PHP: Finding a starting position

When working with strings, you often need to determine whether one string — a substring — is contained in another, and if it is, where. For this, PHP has the mb_strpos() function. It searches for the position of the first occurrence of one string in another:

<?php

print_r(mb_strpos('Валар Моргулис', 'Моргулис')); // => 6

The function returned 6 — the index of the letter М, where the substring begins. Indexes, as usual, are counted from zero:

<?php

print_r(mb_strpos('Валар Моргулис', 'Валар')); // => 0

Here it returned 0: the substring was found at the very beginning of the string.

If the substring isn't found, mb_strpos() returns the special value false ("false"). When printed with print_r(), it looks like an empty string:

<?php

print_r(mb_strpos('Валар Моргулис', 'Дракарис')); // =>

There's a hidden trap here that beginners often fall into: the result 0 ("found at the beginning of the string") is easy to confuse with false ("not found at all"). To tell them apart, you need a strict comparison — we'll cover it in the lessons about logic. For now, just remember: mb_strpos() answers the question "where?", not "is there?".

Instructions

In the program, you need to cut the section name out of the address https://hexlet.io/courses. The first step is to find out where it begins.

Print the position at which the substring courses starts in the string https://hexlet.io/courses. Use the mb_strpos() function.

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Found a bug? Have something to add? Pull requests are welcome!