Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook 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.

    Advertisement

    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

    PHP Arrays: A Beginner’s Guide

    Running Laravel Queue Worker as a Systemd Service

    How to Change PHP Session Timeout

    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
    • Configure Postfix to Use Gmail SMTP on Ubuntu & Debian
    • PHP Arrays: A Beginner’s Guide
    • Deploying Flask Application on Ubuntu (Apache+WSGI)
    • OpenSSL: Working with SSL Certificates, Private Keys and CSRs
    • How to Create and Read List in Python
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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