Current Location: Home> Latest Articles> stripos and substr jointly extract substr

stripos and substr jointly extract substr

M66 2025-05-31

In PHP, stripos and substr are two commonly used string processing functions, which are used to find the first occurrence position of a string and to extract substrings from the specified position respectively. By combining these two functions, we can easily extract the content after the specified position in a certain string.

Stripos function

Stripos is used to find the location of a specified string in another string. Its syntax is as follows:

 stripos($haystack, $needle, $offset);
  • $haystack is the string being searched for.

  • $needle is the string to be looked for.

  • $offset is an optional parameter that sets the starting position of the lookup.

stripos returns the position where the string $needle first appears in $haystack (counting starting from 0). If the specified string is not found, it returns false .

substr function

substr is used to intercept the specified part of a string. Its syntax is as follows:

 substr($string, $start, $length);
  • $string is the string to be intercepted.

  • $start is the start position of the intercept (counting from 0).

  • $length is the number of characters to be intercepted. If omitted, the end of the string will be intercepted by default.

Use stripos and substr in combination

Suppose we have a string and want to extract content from a specific location, we can use stripos to find the location first, and then use substr to extract the rest from that location.

Sample code

 <?php
$string = "Welcome to our official website:https://www.m66.net,Get more information。";
$search = "https://";

// use stripos Find string "https://" Location
$position = stripos($string, $search);

if ($position !== false) {
    // use substr from "https://" 之后Location开始提取字符串
    $result = substr($string, $position + strlen($search));
    echo "Extracted content:$result";
} else {
    echo "The specified content was not found";
}
?>

Code explanation

  1. $string is the string we want to process.

  2. $search is the substring we are looking for, i.e. "https://" .

  3. stripos($string, $search) is used to find the location where "https://" appears for the first time in $string . Note that stripos returns the index of the position where the string first appears.

  4. If the location is found, we use substr($string, $position + strlen($search)) to extract everything from that location.

  5. strlen($search) is used to calculate the length of the search string to ensure that the content is extracted from the end of the substring.

Output result

 Extracted content:www.m66.net,Get more information。

In this way, we can easily extract the content after the specified location in the URL.

Summarize

By combining stripos and substr , we can find the specified position in the string very conveniently and extract the content after that position. This method is very effective whether it is processing URLs, paths, or other string data.