PHP · December 19, 2023

Php Tip: Use substr() to return a part of a string

Php Tip: Use substr() to return a part of a string

When working with strings in PHP, it is often necessary to extract a specific portion of a string. The substr() function in PHP allows you to do just that. In this article, we will explore how to use the substr() function effectively and efficiently.

Understanding the substr() function

The substr() function in PHP is used to extract a part of a string based on the specified start and length parameters. It takes two mandatory parameters and an optional third parameter:

  • string: The input string from which you want to extract a portion.
  • start: The starting position from where the extraction should begin. If the start parameter is positive, counting starts from the beginning of the string. If it is negative, counting starts from the end of the string.
  • length (optional): The number of characters to extract from the string. If not specified, the substr() function will extract all characters from the start position to the end of the string.

Examples of using substr()

Let's look at some examples to understand how to use the substr() function:

$string = "Hello, World!";

// Extract the first 5 characters
$substring1 = substr($string, 0, 5);
echo $substring1; // Output: Hello

// Extract the last 6 characters
$substring2 = substr($string, -6);
echo $substring2; // Output: World!

In the first example, we use substr($string, 0, 5) to extract the first 5 characters of the string. The output is "Hello".

In the second example, we use substr($string, -6) to extract the last 6 characters of the string. The output is "World!".

Using substr() in practical scenarios

The substr() function is particularly useful in scenarios where you need to manipulate or extract specific parts of a string. Here are a few practical examples:

Extracting a domain name from a URL

$url = "https://www.example.com";
$domain = substr($url, strpos($url, "://") + 3);
echo $domain; // Output: www.example.com

In this example, we use substr() in combination with strpos() to extract the domain name from a URL. The output is "www.example.com".

Masking sensitive information

$creditCardNumber = "1234567890123456";
$maskedNumber = substr($creditCardNumber, -4);
$maskedNumber = str_pad($maskedNumber, strlen($creditCardNumber), "*", STR_PAD_LEFT);
echo $maskedNumber; // Output: ************3456

In this example, we use substr() to extract the last 4 digits of a credit card number and then mask the rest of the digits with asterisks. The output is "************3456".

Conclusion

The substr() function in PHP is a powerful tool for extracting specific portions of a string. By understanding how to use this function effectively, you can manipulate and extract data from strings with ease. Whether you need to extract parts of a URL or mask sensitive information, the substr() function is a valuable asset in your PHP toolkit.

For more information on PHP and VPS hosting solutions, visit Server.HK.