• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

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

December 19, 2023

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.

Recent Posts

  • How to Host a Python Flask or Django Application on Hong Kong VPS (2026)
  • How to Set Up WireGuard VPN on a Hong Kong VPS: Step-by-Step Guide 2026
  • Hong Kong VPS vs DigitalOcean: Cost, Performance, and China Routing Compared (2026)
  • VPS Hosting vs Shared Hosting: Why the Upgrade Is Worth It for Asia-Facing Websites
  • Hong Kong VPS vs Google Cloud Asia: Which Delivers Better China Performance in 2026?

Recent Comments

  1. vibramycin injection on How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026
  2. allopurinol for gout on CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  3. antibiotics online purchase on How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  4. linezolid cost oral on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  5. metoprolol generic on Hong Kong VPS vs Japan VPS: Head-to-Head for Asia-Pacific Deployments in 2026

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot