• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to check if string contains specific word in PHP

Written by Rahul, Updated on June 25, 2019

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';
}
?>

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

1 Comment

  1. Avatar Basant Reply
    October 17, 2019 at 7:22 am

    Nice Man what i need i just got here.

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
  • How to Install .NET Core on Debian 10 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy