PHP Version
4+
$str = 'Hello, World!';
$substring = 'World';

$pos = strpos($str, $substring);

if ($pos !== false) {
    echo "Found at position: $pos";
} else {
    echo 'Not found.';
}

// Output: Found at position: 7

/*
 * Note:
 * In PHP, string indexes start at 0.
 * The index of the first character is 0, and the second character is 1.
 */
strpos(string $haystack, string $needle, int $offset = 0): int|false
$str = 'Hello, World!';
$substring = 'world';

$pos = strpos($str, $substring);

if ($pos === false) {
    echo "Cannot find 'world!' because the case does not match.";
} else {
    echo "'world!' is included in the string.";
}

// Output: Cannot find 'world!' because the case does not match.
$newstring = 'abcdef ghijk';
$pos = strpos($newstring, 'a');

var_dump($pos); // int(0)
$str = 'Hello, World!';
$substring = '';

$pos = strpos($str, $substring); // Warning: strpos(): Empty needle in
$str = 'Hello, World!';
$substring = 'Hello';

$pos = strpos($str, $substring);

var_dump($pos); // int(0)

if ($pos === false) {
    echo "The string 'Hello' was not found.";
} else {
    echo "The string 'Hello' is included in the text.";
}

// Output: The string 'Hello' is included in the text.
$str = 'Nice to meet you. Welcome!';
$substring = 'Welcome';

$pos = strpos($str, $substring);

if ($pos !== false) {
    echo "Found at position: $pos";
} else {
    echo "Not found.";
}

// Output: Found at position: 18 <= unexpected result for multi-byte strings
$file_path = '/var/www/html/project/files/document.txt';
$directory_name = '/var/www/html/project';

// Check if the file path contains a specific directory
if (strpos($file_path, $directory_name) !== false) {
    echo 'The file belongs to the specified directory.';
} else {
    echo 'The file does not belong to the specified directory.';
}

// Output: 'The file belongs to the specified directory.'

// Extract the file name from the path
$file_name = substr($file_path, strrpos($file_path, '/') + 1);
echo "File name: $file_name";

// Output: "File name: document.txt"