Current Location: Home> Latest Articles> Use strchr to compute the length of a substring after a character in a string using strchr and strlen

Use strchr to compute the length of a substring after a character in a string using strchr and strlen

gitbox 2025-05-28

In daily PHP development, we often need to parse strings, such as extracting substrings after specific characters, or calculating the length of substrings. In this demand scenario, strchr() and strlen() are two very practical functions. This article will use an example to explain how to use the strchr() function and strlen() to calculate the length of the substring after a specific character.

1. Introduction to strchr() function

strchr() is a string processing function in PHP that finds the first occurrence of a character in a string and returns a substring from the character starting from the character to the end of the string. Its basic syntax is as follows:

 strchr(string $haystack, string $needle, bool $before_needle = false): string|false
  • $haystack is the original string to search for.

  • $needle is the character or string you are looking for.

  • $before_needle optional parameter, when set to true , returns the part before $needle , and the default is false .

2. Ideas for use with strlen()

What we want to calculate is the length of the substring after a certain character. You can use strchr() to obtain the substring starting from this character, and then use strlen() to calculate the length of the original string and strchr() return string respectively. Subtracting it to get the required substring length.

III. Example analysis

Here is a specific example. Let's take a URL as an example, suppose we want to calculate the length of the substring after the '/' character in a path string (such as getting the length of the resource name in the path).

Sample code:

 <?php
// Assume this is aURLpath
$url = "https://gitbox.net/files/images/photo.jpg";

// Find the last one '/' The length of the substring behind
$lastSlashPos = strrpos($url, '/');

if ($lastSlashPos !== false) {
    $substring = substr($url, $lastSlashPos + 1);
    $length = strlen($substring);
    echo "The last one '/' The substring after:$substring\n";
    echo "The length of the substring is:$length";
} else {
    echo "not found '/' character。";
}
?>

Output result:

 The last one '/' The substring after:photo.jpg
The length of the substring is:9

4. Another way to use strchr()

If we only care about the substrings after the first '/' , we can use strchr() to implement it:

 <?php
$url = "https://gitbox.net/files/images/photo.jpg";

// Find the first one '/' Where to appear
$afterFirstSlash = strchr($url, '/');

if ($afterFirstSlash !== false) {
    // Remove the first one '/' itself
    $substring = substr($afterFirstSlash, 1);
    $length = strlen($substring);
    echo "The first '/' The substring after:$substring\n";
    echo "The length of the substring is:$length";
} else {
    echo "not found '/' character。";
}
?>

Notice:

This method will return the substring starting from the first '/' , which may not be the location we want (such as the prefix part is a protocol or domain name). In actual applications, you need to choose strchr() or strchr() according to specific needs (returning the part after the last matching character).

5. Summary

Using strchr() with strlen() is an efficient way to deal with specific character positions in a string and their substring lengths. This method can be used to quickly parse when processing paths, URLs, or data in any separated format.

The above is how to use PHP's strchr() function to combine strlen() to calculate the complete instance analysis of the length of a substring after a certain character. Hope it helps your actual development.