• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / How to Check If String Contains Substring in PHP

How to Check If String Contains Substring in PHP

December 11, 2020 Leave a Comment

PHP provides the strpos() function to check if a string contains a specific substring or not. The strpos() function returns the position of the first occurrence of a substring in a string. If substring not found, it will return false.

For example, let’s say you were checking the string “I love to program” to see if the word “program” was contained in the string.

$string = "I love to program!";
$substring = "program";

$result = strpos($string,$substring)   /* $result = 10 */

Finding if a String is in an Array Using the strpos() Function in PHP

Now, let’s say I wanted to loop over an array in PHP and see if a string is contained in that array.

I could write a function such as the one below, and use the strpos() to see if the string is in the array of strings.

function contains($str, array $arr)
{
    foreach($arr as $a) {
        if (strpos($str,$a) !== false) return true;
    }
    return false;
}

$terms = ["frog", "dog", "elephant", "giraffe"];

$result = contains("frog",$terms) /* $result = true */
$result = contains("lamp",$terms) /* $results = false */

The Difference Between the strpos() and stripos() Functions in PHP

The strpos() function finds the position of a string and the stripos() function is the same as the strpos() function but is used for a case-insensitive search. Both return the position of the first occurrence of a string inside another string.

For example, you can see the difference in the following code of the results of using strpos() vs. stripos():

$string = "I love to program!";
$substring1 = "program";
$substring2 = "PROGRAM";

$result = strpos($string,$substring1)   /* $result = 10 */
$result = strpos($string,$substring2)   /* $result = false */
$result = stripos($string,$substring1)   /* $result = 10 */
$result = stripos($string,$substring2)   /* $result = 10 */

The str_contains() function in PHP 8

In PHP 8, the str_contains() function was introduced which returns true if the substring is in the string.

$string = "I love to program!";
$substring = "program";

$result = str_contains($string,$substring)   /* $result = true */

Using str_contains() can be useful if you don’t care about where the substring is in the string – only if it is contained in the string.

Other Articles You'll Also Like:

  • 1.  php Square Root with sqrt Function
  • 2.  Using strtoupper() in php to Convert String to Uppercase
  • 3.  php switch case – How to Use Switch…Case Statements in php
  • 4.  How to Get Current System IP Address in php
  • 5.  php var_dump() Function – Print Information about Variable Type and Value
  • 6.  php Rename File – Renaming Files and Directories Easily
  • 7.  php atanh – Find Hyperbolic Arctangent of Number Using atanh() Function
  • 8.  php array_pop() – Remove Last Element from Array
  • 9.  Remove Last Character from String Using php
  • 10.  php Delete Files with php Unlink Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy