• 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 Function:strstr

December 19, 2023

PHP Function: strstr

In PHP, the strstr function is a powerful tool that allows developers to search for a specific substring within a given string. This function is particularly useful when working with text manipulation and data extraction tasks. In this article, we will explore the various features and use cases of the strstr function.

Syntax

The syntax of the strstr function is as follows:

strstr(string $haystack, mixed $needle, bool $before_needle = false): string|false

The strstr function takes three parameters:

  • $haystack: The string to search within.
  • $needle: The substring to search for.
  • $before_needle (optional): If set to true, the function returns the part of the haystack before the first occurrence of the needle. If set to false (default), it returns the part of the haystack from the first occurrence of the needle onwards.

The function returns the portion of the haystack string starting from the first occurrence of the needle, or false if the needle is not found.

Examples

Let’s explore some examples to understand how the strstr function works:

Example 1: Basic Usage

$string = "Hello, World!";
$substring = "World";

$result = strstr($string, $substring);

echo $result; // Output: "World!"

In this example, the strstr function searches for the substring “World” within the string “Hello, World!”. Since the substring is found, it returns the portion of the string starting from the first occurrence of the substring, which is “World!”.

Example 2: Case Sensitivity

$string = "Hello, World!";
$substring = "world";

$result = strstr($string, $substring);

echo $result; // Output: false

The strstr function is case-sensitive. In this example, the substring “world” is not found within the string “Hello, World!” because the case does not match. Therefore, the function returns false.

Example 3: Returning Part Before Needle

$string = "Hello, World!";
$substring = "World";

$result = strstr($string, $substring, true);

echo $result; // Output: "Hello, "

By setting the third parameter of the strstr function to true, we can retrieve the part of the string before the first occurrence of the needle. In this example, it returns “Hello, “.

Conclusion

The strstr function in PHP is a versatile tool for searching and extracting substrings within a given string. It provides developers with the ability to perform various text manipulation tasks efficiently. By understanding the syntax and examples provided in this article, you can leverage the power of the strstr function in your PHP projects.

For more information about VPS hosting solutions, consider exploring Server.HK, a leading VPS hosting company that offers top-notch services.

Recent Posts

  • How to Configure FirewallD in CentOS Stream: From Essential to Production-Grade
  • Installing Docker on CentOS: A Practical Setup Guide (CentOS Stream 9/10 – 2026)
  • How to Secure a CentOS Server: 15 Essential Hardening Techniques (CentOS Stream 9/10 – 2026)
  • CentOS End of Life (EOL): What It Means and Migration Options in 2026
  • How to Configure a LAMP Stack on CentOS Stream for Production

Recent Comments

No comments to show.

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