Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Programming»PHP»How to check if string contains specific word in PHP

    How to check if string contains specific word in PHP

    By RahulJune 25, 20191 Min Read

    This tutorial will help you to check if a string contains any substring in PHP programming language. For example, you want to run a specific line of code only if an input string contains another substring in it.

    Here is a sample PHP programme, which will evaluate to true because the main string $str contains the substring ‘TecAdmin‘ in it. This will print “true”.

    1
    2
    3
    4
    5
    6
    7
    <?php
    $str = 'Welcome to Tecadmin';
     
    if (strpos($str, 'Tecadmin') !== false) {
        echo 'true';
    }
    ?>

    Another PHP program will evaluate to false because the main string $str doesn’t contains the substring ‘Hello‘ in it. This will nothing print.

    1
    2
    3
    4
    5
    6
    7
    8
    <?php
    $str = 'Welcome to Tecadmin';
    $substr = "Hello";
     
    if (strpos($str, $substr) !== false) {
        echo 'true';
    }
    ?>

    PHP string substring
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Capitalize the First Letter in a String Using JavaScript

    How to Read InputStream into a String in Java

    Java Program to Reverse a String

    View 1 Comment

    1 Comment

    1. Basant on October 17, 2019 7:22 am

      Nice Man what i need i just got here.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Difference Between Full Virtualization vs Paravirtualization
    • Virtualization vs. Containerization: A Comparative Analysis
    • Using .env Files in Django
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.