• 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:stristr

December 19, 2023

PHP Function: stristr

In PHP, the stristr function is a powerful tool that allows developers to search for a specific substring within a given string, regardless of the case sensitivity. This function is particularly useful when working with user input or when manipulating strings in various applications.

Syntax

The syntax for the stristr function is as follows:

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

The stristr 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), the function returns the part of the haystack from the first occurrence of the needle onwards.

Return Value

The stristr function returns the portion of the haystack string starting from the first occurrence of the needle and up to the end of the string. If the needle is not found, the function returns false.

Example Usage

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

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

$result = stristr($string, $substring);
echo $result; // Output: "World!"

In this example, the stristr function successfully finds the substring “world” within the haystack string “Hello, World!” and returns the portion of the string starting from the first occurrence of the substring.

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

$result = stristr($string, $substring);
echo $result; // Output: "World!"

Even though the needle is in uppercase, the stristr function is case-insensitive by default. It still successfully finds the substring “WORLD” within the haystack string “Hello, World!” and returns the portion of the string starting from the first occurrence of the substring.

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

$result = stristr($string, $substring, true);
echo $result; // Output: "Hello, "

By setting the third parameter to true, the stristr function returns the portion of the haystack string before the first occurrence of the needle. In this example, it returns “Hello, “.

Summary

The stristr function in PHP is a versatile tool for searching and manipulating strings. It allows developers to find substrings within a given string, regardless of case sensitivity. By understanding how to use this function effectively, developers can enhance their string manipulation capabilities and create more robust applications.

If you are interested in exploring more about VPS hosting solutions, consider checking out Server.HK. They offer top-notch VPS hosting services with reliable performance and excellent customer support.

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