How to check if string contains specific word in PHP

Channel: Linux
Abstract: php $str = 'Welcome to Tecadmin'> Another PHP program will evaluate to false because the main string $str doesn’t contains the substring ‘Hello‘ in it

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」.

<?php $str = 'Welcome to Tecadmin'; if (strpos($str, 'Tecadmin') !== false) { echo 'true'; } ?>1234567<?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.

<?php $str = 'Welcome to Tecadmin'; $substr = "Hello"; if (strpos($str, $substr) !== false) { echo 'true'; } ?>12345678<?php$str = 'Welcome to Tecadmin';$substr = "Hello"; if (strpos($str, $substr) !== false) {    echo 'true';}?>

Ref From: tecadmin

Related articles